package com.soft.hwx.scdapp;

import android.Manifest;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.soft.hwx.logutillib.LogConfig;
import com.soft.hwx.logutillib.LogUtil;
import com.soft.hwx.scdapp.main.ScoutHost;
import com.soft.hwx.scdapp.main.ScoutHostInterface;
import com.soft.hwx.scdapp.msg.HostRealseSearchRspMsg;
import com.soft.hwx.scdapp.msg.HostSearchRspMsg;
import com.soft.hwx.scdapp.msg.HostSelfCheckRspMsg;
import com.soft.hwx.scdapp.msg.HostSetArgsMsg;
import com.soft.hwx.scdapp.msg.HostSetArgsRspMsg;
import com.soft.hwx.scdapp.msg.HostWorkCmd;
import com.soft.hwx.scdapp.msg.HostWorkMsg;
import com.soft.hwx.scdapp.msg.HostWorkStatus;
import com.soft.hwx.scdapp.msg.RawMsg;
import com.soft.hwx.scdapp.socket.UdpHelper;
import com.soft.hwx.scdapp.usb.UsbSerialManager;
import java.math.BigDecimal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;
import static com.soft.hwx.scdapp.HexStrHelper.convertHexToString;
import static com.soft.hwx.scdapp.SDCardHelper.file2String;
import static com.soft.hwx.scdapp.SDCardHelper.getSDCardPrivateFilesDir;
import static com.soft.hwx.scdapp.SDCardHelper.loadFileFromSDCard;
import static com.soft.hwx.scdapp.SDCardHelper.readIp;
import static com.soft.hwx.scdapp.SDCardHelper.saveFileToSDCardPrivateFilesDir;
import static com.soft.hwx.scdapp.SDCardHelper.saveIp;
import static com.soft.hwx.scdapp.SDCardHelper.string2File;
import static com.soft.hwx.scdapp.StrHelper.strCovJwd;
import static com.soft.hwx.scdapp.main.FileSelection.getPath;
import static com.soft.hwx.scdapp.msg.HostWorkCmd.releaseSearch;
import static com.soft.hwx.scdapp.msg.HostWorkResult.releaseSearchSuccess;
import static com.soft.hwx.scdapp.msg.HostWorkResult.selfCheckDoing;
import static com.soft.hwx.scdapp.msg.HostWorkResult.selfCheckError;
import static com.soft.hwx.scdapp.msg.HostWorkResult.selfCheckWell;
import static com.soft.hwx.scdapp.msg.HostWorkResult.setDoing;
import static com.soft.hwx.scdapp.msg.HostWorkResult.setError;
import static com.soft.hwx.scdapp.msg.HostWorkResult.setWell;


public class MainActivity extends AppCompatActivity implements View.OnClickListener,ScoutHostInterface {
    // Used to load the 'native-lib' library on application startup.
    private TextView f1;
    private TextView f2;
    private TextView f3;
    private F1Fragment f1Fragment;
    private F2Fragment f2Fragment;
    private F3Fragment f3Fragment;
    private Fragment mContent;
    private FrameLayout layout;
    private  ScoutHost scoutHost;

    private  UsbSerialManager mUsbSerialManager;
    //读写权限
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;
    //显示主机通讯消息
    private static int M_HANDLE_WHAT_CODE_HOST_RAW_MSG = 100;
    //显示外设状态
    private static int M_HANDLE_WHAT_CODE_EXT_DEV_STATUS = 101;

    boolean IsSelfcheckNormal = false;

    private Spinner spinner;
    private List<String> data_list;
    private ArrayAdapter<String> arr_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //设置一个布局
        setContentView(R.layout.activity_main);

        android.support.v7.app.ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null) {
            actionBar.hide();
        }

        //文件读写权限
        applyPermission();

        //初始日志
        initLog();

        //初始USB
        initUsbControl();

        //初始化控件
        initView();

        //初始化Fragment
        initFragment();

        //相对安卓终端来说 远程主机ip地址和端口设置
        String ip_addr;
        int port;
        Context content =getApplicationContext();
        Map<String,String> mMap= new HashMap<String,String>();
        mMap = readIp(content);
        if(mMap == null){
            //默认
            ip_addr = "192.168.1.116";
            port = 8080;
        }else{
            ip_addr = mMap.get("ip_addr");
            port = Integer.parseInt(mMap.get("port"));
        }

        scoutHost = new ScoutHost(this,ip_addr,port);
        scoutHost.Init();


/*
        String bdStr = convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+


                convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+

                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0")+
                convertHexToString("0");

*/

/*

        String bdStr = convertHexToString("41")+
                convertHexToString("2c")+

                convertHexToString("41")+
                convertHexToString("2c")+

                convertHexToString("31")+
                convertHexToString("31")+
                convertHexToString("36")+
                convertHexToString("31")+
                convertHexToString("34")+
                convertHexToString("2e")+
                convertHexToString("35")+
                convertHexToString("30")+
                convertHexToString("38")+
                convertHexToString("30")+
                convertHexToString("32")+
                convertHexToString("35")+
                convertHexToString("36")+
                convertHexToString("2c")+


                convertHexToString("45")+
                convertHexToString("2c")+

                convertHexToString("34")+
                convertHexToString("30")+
                convertHexToString("30")+
                convertHexToString("34")+
                convertHexToString("2e")+
                convertHexToString("32")+
                convertHexToString("30")+
                convertHexToString("30")+
                convertHexToString("36")+
                convertHexToString("35")+
                convertHexToString("35")+
                convertHexToString("37")+
                convertHexToString("2c")+

                convertHexToString("4e")+
                convertHexToString("2c")+

                convertHexToString("32")+
                convertHexToString("32")+
                convertHexToString("2c")+

                convertHexToString("31")+
                convertHexToString("33")+
                convertHexToString("33")+
                convertHexToString("2e")+
                convertHexToString("38")+
                convertHexToString("32")+
                convertHexToString("36")+
                convertHexToString("37")+
                convertHexToString("0");

        Log.d("MainActivity", "北斗信号:"+bdStr);

*/
/*
        if(bdStr.equals("")||(bdStr.length()==0)){
            return;
        }
        String substring = bdStr.substring(0, bdStr.length() - 1);
        String[] split = substring.split(",");//以逗号分割

        Log.d("MainActivity", "北斗信号分隔后长度:"+split.length);

        String isLocatedTv = "";
        if(split[0].equals("A")){
            isLocatedTv +="已定位";
        }else if(split[0].equals("V")){
            isLocatedTv +="未定位";
        }

        if(split[1].equals("A")){
            isLocatedTv +="已定向";
        }else if(split[1].equals("V")){
            isLocatedTv +="未定向";
        }

        Log.d("MainActivity", "定位定向"+isLocatedTv);

        /*
        String subJd = split[2].substring(0, split[2].length() - 1);
        //System.out.println(substring);
        String[] splitJd = subJd.split("\\.");//以逗号分割
        Log.d("MainActivity", "经度整数"+splitJd[0]);

        String jdf = splitJd[0].substring(splitJd[0].length() -2,splitJd[0].length());   //截取后两位
        Log.d("MainActivity", "经度整数后两位"+jdf);

        String jdd = splitJd[0].substring(0,splitJd[0].length()-2);   //截掉后两位
        Log.d("MainActivity", "经度整数截掉后两位"+jdd);


        Log.d("MainActivity", "经度小数点"+splitJd[1]);
        BigDecimal bdjd1 = new BigDecimal(splitJd[1]);

        Log.d("MainActivity", "经度小数转换后的值"+bdjd1.divide(new BigDecimal(100)).multiply(new BigDecimal(60)).toString());

        String jdm = bdjd1.divide(new BigDecimal(100)).multiply(new BigDecimal(60)).toString();
        jdm = jdm.substring(0,2);
        Log.d("MainActivity", "经度转换后的最终值"+jdd+"°"+jdf+"'"+jdm+"''");
        */
/*
        String sjd = "";
        if(split[3].equals("E")){
            sjd+="东经";
        }else if(split[3].equals("W")){
            sjd+="西经";
        }
        sjd+=strCovJwd(split[2]);
        Log.d("MainActivity", "经度:"+sjd);

        String swd = "";
        if(split[5].equals("N")){
            sjd+="北纬";
        }else if(split[5].equals("S")){
            sjd+="南纬";
        }
        swd+=strCovJwd(split[4]);


        Log.d("MainActivity", "纬度:"+swd);



        Log.d("MainActivity", "卫星数:"+split[6]);

        /*
        BigDecimal bdDirAngle1 = new BigDecimal(split[7]);
        BigDecimal bdDirAngle2 = new BigDecimal("88.4463");
        System.out.println("BigDecimal:"+bdDirAngle1.subtract(bdDirAngle2).floatValue());*/
/*
        float fDirAngle;
        fDirAngle = Float.valueOf(split[7])-90+1.5537f;
        if(fDirAngle<0){
            fDirAngle += 360;
        }
        String dirAngle = String.valueOf(fDirAngle);
        Log.d("MainActivity", "方位角:"+dirAngle);


        /*
        for (int i = 0; i < split.length; i++){
            System.out.println("数据" + split[i]);
        }*/

    }

    //申请权限
    private void applyPermission(){

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);
                Toast.makeText(getApplicationContext(),"申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i],Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void initLog(){
        LogConfig logConfig = new LogConfig.Builder(getApplicationContext())
                .setLogLevel(LogUtil.VERBOSE)
                .setNeedSaveToDefaultFile(true)
                .prefix("")
                .suffix("")
                .defaultTag("LOG")
                .dirPath("/scdApp")
                .hostIp("192.168.1.48")
                .addSaveRule("MainActivity", true)
                .addSaveRule("Feed", true)
                .addSaveRule("qqq", false)
                .build();
        LogUtil.init(logConfig);
    }

    /**
     * 初始化USB
     */
    private void initUsbControl() {
        mUsbSerialManager = new UsbSerialManager(this);
        mUsbSerialManager.initUsbControl();
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter usbFilter = new IntentFilter();
        usbFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        usbFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbReceiver, usbFilter);
        mUsbSerialManager.onDeviceStateChange();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mUsbSerialManager.onPause();
        unregisterReceiver(mUsbReceiver);
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 用于检测usb插入状态的BroadcasReceiver
     */
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                //设备插入
                mUsbSerialManager.initUsbControl();
                mUsbSerialManager.onDeviceStateChange();
                LogUtil.e("ACTION_USB_DEVICE_ATTACHED USB 设备插入");
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                //设备移除
                mUsbSerialManager.onPause();
                LogUtil.e("ACTION_USB_DEVICE_DETACHED USB 设备移除");
            }
        }
    };

    private void initFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (f1Fragment != null && f1Fragment.isAdded()) {
            transaction.remove(f1Fragment);
        }
        if (f2Fragment != null && f2Fragment.isAdded()) {
            transaction.remove(f2Fragment);
        }
        if (f3Fragment != null && f3Fragment.isAdded()) {
            transaction.remove(f3Fragment);
        }
        transaction.commitAllowingStateLoss();
        f1Fragment = null;
        f2Fragment = null;
        f3Fragment = null;
        f3.performClick();
        f2.performClick();
        f1.performClick();
    }

    private void initView() {
        f1 = findViewById(R.id.main_f1);
        f2 = findViewById(R.id.main_f2);
        f3 = findViewById(R.id.main_f3);
        layout = findViewById(R.id.main_layout);

        f1.setOnClickListener(this);
        f2.setOnClickListener(this);
        f3.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        View v = getCurrentFocus();
        if(v!=null){//当输入框有光标 键盘被弹出时  隐藏弹出的键盘
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        switch (view.getId()) {

            case R.id.main_f1:
                switchContent(f1);
                break;
            case R.id.main_f2:
                switchContent(f2);
                break;
            case R.id.main_f3:
                switchContent(f3);
                break;
        }
    }

    public void switchContent(View view) {
        Fragment fragment;
        if (view == f1) {
            if (f1Fragment == null) {
                f1Fragment = new F1Fragment();
            }
                fragment = f1Fragment;
            } else if (view == f2) {
                if (f2Fragment == null) {
                    f2Fragment = new F2Fragment();
                }
                fragment = f2Fragment;
            } else if (view == f3) {
                if (f3Fragment == null) {
                    f3Fragment = new F3Fragment();
                }
                fragment = f3Fragment;
            } else {
                return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mContent == null) {
            transaction.add(layout.getId(), fragment).commit();
            mContent = fragment;
        }
        if (mContent != fragment) {
            if (!fragment.isAdded()) {
                transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss();
            } else {
                transaction.hide(mContent).show(fragment).commitAllowingStateLoss();
            }
            mContent = fragment;
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent i = new Intent("setting.LAUNCH");
            startActivity(i);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //
            if (msg.what == M_HANDLE_WHAT_CODE_HOST_RAW_MSG) {
                //主机通信消息
                showMsgUI((RawMsg)msg.obj);
                /*
                byte[] buf = ((RawMsg) msg.obj).getBuf();
                String strBuf = "";
                for(int i=0;i<buf.length;i++){
                    strBuf += String.format(" %02x",buf[i]);
                }
                Log.d("MainActivity", "报文内容:"+strBuf);*/

            }else if(msg.what == M_HANDLE_WHAT_CODE_EXT_DEV_STATUS){
                //更新外设设备状态
            }
        }
    };

    @Override
    public void showMes(RawMsg msg)
    {
        Message message = Message.obtain();
        message.obj = msg;
        message.what = M_HANDLE_WHAT_CODE_HOST_RAW_MSG;
        mHandler.sendMessage(message);
    }


    public void showMsgUI(RawMsg msg){

        //或者根据报文工作状态 判断报文类型
        switch (msg.workStatus){
            case HostWorkStatus.selfCheck:
                Log.d("MainActivity", "自检.....");
                showHostSelfCheckRspMsgUI((HostSelfCheckRspMsg)msg);
                break;
            case HostWorkStatus.setArgs:
                showHostSetArgsRspMsgUI((HostSetArgsRspMsg)msg);
                break;
            case HostWorkStatus.search:
                showHostSearchRspMsgUI((HostSearchRspMsg)msg);
                break;
            case HostWorkStatus.releaseSearch:
                showHostRealseSearchRspMsgUI((HostRealseSearchRspMsg)msg);
                break;
        }
    }

    /*====================================================================================================*/
    /*==============================================页面1=================================================*/
    /*====================================================================================================*/

    private void showHostSelfCheckRspMsgUI(HostSelfCheckRspMsg msg){
        TextView f1Tv = (TextView) findViewById(R.id.f1Tv);
        if(msg.getResult()== (byte)selfCheckWell){
            Log.d("MainActivity", "自检正常");
            IsSelfcheckNormal = true;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"自检正常");
        }else if(msg.getResult() == (byte)selfCheckError){
            Log.d("MainActivity", "自检故障");
            IsSelfcheckNormal = false;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"自检故障");
        }else if(msg.getResult() == (byte)selfCheckDoing){
            Log.d("MainActivity", "正在自检");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"正在自检");
        }else{
            Log.d("MainActivity", "未知结果");
            IsSelfcheckNormal = false;
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"未知结果");
        }
    }

    private void showHostSetArgsRspMsgUI(HostSetArgsRspMsg msg){
        TextView f1Tv = (TextView) findViewById(R.id.f1Tv);
        if(msg.getResult()== (byte)setDoing){
            Log.d("MainActivity", "正在装订");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"正在装订");
        }else if(msg.getResult() == (byte)setWell){
            Log.d("MainActivity", "装订完成");
            //开始装订和当前时刻
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +" 装订完成");
        }else if(msg.getResult() == (byte)setError){
            Log.d("MainActivity", "装订故障");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"装订故障");
        }else{
            Log.d("MainActivity", "未知结果");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            f1Tv.setText(date +"未知结果");
        }
    }

    public void showTitle(String str){
        AlertDialog.Builder builder  = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示" ) ;
        builder.setMessage(str);
        builder.setPositiveButton("确认" ,  null );
        builder.show();
    }

    public void sendParaBind(View view){

        TextView f1Tv = (TextView) findViewById(R.id.f1Tv);
        //f1Tv.getBackground().setAlpha(0);//0~255透明度值
        int Interval_num = f1Fragment.get_spinner_item();

        View v = getCurrentFocus();
        if(v!=null){//隐藏键盘
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        Log.d("MainActivity", "发送参数装订命令");
        AlertDialog.Builder builder  = new AlertDialog.Builder(MainActivity.this);
        byte [] buf = new byte[0x64];
        HostSetArgsMsg ArgsMsg = new HostSetArgsMsg(HostWorkCmd.setArgs,buf);

        if(Interval_num == 0){
            ArgsMsg.t1Sn = 1;
            Log.d("MainActivity", "下拉框值:"+"1");
        }else if(Interval_num == 1){
            ArgsMsg.t1Sn = 1;
            Log.d("MainActivity", "下拉框值:"+"2");
        }else if(Interval_num == 2){
            ArgsMsg.t1Sn = 1;
            Log.d("MainActivity", "下拉框值:"+"3");
        }

        //IsSelfcheckNormal = true;
        //先判断自检是否正常
        /*暂时不做判断
        if(IsSelfcheckNormal == false){
            Log.d("MainActivity", "自检不正常");
            builder.setTitle("提示" ) ;
            builder.setMessage("自检不正常");
            builder.setPositiveButton("确认" ,  null );
            builder.show();
            return;
        }*/

        EditText et1FreqUpper = (EditText) findViewById(R.id.et1FreqUpper);
        EditText et1FreqLower = (EditText) findViewById(R.id.et1FreqLower);
        EditText et1PulseWidthUpper = (EditText) findViewById(R.id.et1PulseWidthUpper);
        EditText et1PulseWidthLower = (EditText) findViewById(R.id.et1PulseWidthLower);
        EditText et1RepeatFreqUpper = (EditText) findViewById(R.id.et1RepeatFreqUpper);
        EditText et1RepeatFreqLower = (EditText) findViewById(R.id.et1RepeatFreqLower);

        EditText et2FreqUpper = (EditText) findViewById(R.id.et2FreqUpper);
        EditText et2FreqLower = (EditText) findViewById(R.id.et2FreqLower);
        EditText et2PulseWidthUpper = (EditText) findViewById(R.id.et2PulseWidthUpper);
        EditText et2PulseWidthLower = (EditText) findViewById(R.id.et2PulseWidthLower);
        EditText et2RepeatFreqUpper = (EditText) findViewById(R.id.et2RepeatFreqUpper);
        EditText et2RepeatFreqLower = (EditText) findViewById(R.id.et2RepeatFreqLower);

        EditText et3FreqUpper = (EditText) findViewById(R.id.et3FreqUpper);
        EditText et3FreqLower = (EditText) findViewById(R.id.et3FreqLower);
        EditText et3PulseWidthUpper = (EditText) findViewById(R.id.et3PulseWidthUpper);
        EditText et3PulseWidthLower = (EditText) findViewById(R.id.et3PulseWidthLower);
        EditText et3RepeatFreqUpper = (EditText) findViewById(R.id.et3RepeatFreqUpper);
        EditText et3RepeatFreqLower = (EditText) findViewById(R.id.et3RepeatFreqLower);

        if(Interval_num == 0){
            if(
                    et1FreqUpper.getText().toString().equals("") ||
                    et1FreqLower.getText().toString().equals("") ||
                    et1PulseWidthUpper.getText().toString().equals("") ||
                    et1PulseWidthLower.getText().toString().equals("") ||
                    et1RepeatFreqUpper.getText().toString().equals("") ||
                    et1RepeatFreqLower.getText().toString().equals("")){
                showTitle("存在输入文本框为空,请输入内容");
                return;
            }
        }else if(Interval_num == 1){
            if(
                    et1FreqUpper.getText().toString().equals("") ||
                    et1FreqLower.getText().toString().equals("") ||
                    et1PulseWidthUpper.getText().toString().equals("") ||
                    et1PulseWidthLower.getText().toString().equals("") ||
                    et1RepeatFreqUpper.getText().toString().equals("") ||
                    et1RepeatFreqLower.getText().toString().equals("") ||
                    et2FreqUpper.getText().toString().equals("") ||
                    et2FreqLower.getText().toString().equals("") ||
                    et2PulseWidthUpper.getText().toString().equals("") ||
                    et2PulseWidthLower.getText().toString().equals("") ||
                    et2RepeatFreqUpper.getText().toString().equals("") ||
                    et2RepeatFreqLower.getText().toString().equals("")){
                showTitle("存在输入文本框为空,请输入内容");
                return;
            }
        }else if (Interval_num == 2){
            if(
                    et1FreqUpper.getText().toString().equals("") ||
                    et1FreqLower.getText().toString().equals("") ||
                    et1PulseWidthUpper.getText().toString().equals("") ||
                    et1PulseWidthLower.getText().toString().equals("") ||
                    et1RepeatFreqUpper.getText().toString().equals("") ||
                    et1RepeatFreqLower.getText().toString().equals("") ||
                    et2FreqUpper.getText().toString().equals("") ||
                    et2FreqLower.getText().toString().equals("") ||
                    et2PulseWidthUpper.getText().toString().equals("") ||
                    et2PulseWidthLower.getText().toString().equals("") ||
                    et2RepeatFreqUpper.getText().toString().equals("") ||
                    et2RepeatFreqLower.getText().toString().equals("") ||
                    et3FreqUpper.getText().toString().equals("") ||
                    et3FreqLower.getText().toString().equals("") ||
                    et3PulseWidthUpper.getText().toString().equals("") ||
                    et3PulseWidthLower.getText().toString().equals("") ||
                    et3RepeatFreqUpper.getText().toString().equals("") ||
                    et3RepeatFreqLower.getText().toString().equals(""))
            {
                showTitle("存在输入文本框为空,请输入内容");
                return;
            }
        }

        if(Interval_num == 0){
            //区间1
            ArgsMsg.t1UpperLimitCarrierFreq = Short.parseShort(et1FreqUpper.getText().toString());
            if((ArgsMsg.t1UpperLimitCarrierFreq<500) || (ArgsMsg.t1UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t1LowerLimitCarrierFreq = Short.parseShort(et1FreqLower.getText().toString());
            if((ArgsMsg.t1LowerLimitCarrierFreq<500) || (ArgsMsg.t1LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et1PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et1PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1UpperLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et1PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et1PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1LowerLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthLower.getText().toString())*100);
            float cpu = Float.parseFloat(et1RepeatFreqUpper.getText().toString());
            float cpl = Float.parseFloat(et1RepeatFreqUpper.getText().toString());
            if ((Float.parseFloat(et1RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et1RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqLower.getText().toString())*5);

            //区间2
            ArgsMsg.t2UpperLimitCarrierFreq = ArgsMsg.t1UpperLimitCarrierFreq;
            ArgsMsg.t2LowerLimitCarrierFreq = ArgsMsg.t1LowerLimitCarrierFreq;
            ArgsMsg.t2UpperLimitPulseWidth = ArgsMsg.t1UpperLimitPulseWidth;
            ArgsMsg.t2LowerLimitPulseWidth = ArgsMsg.t1LowerLimitPulseWidth;
            ArgsMsg.t2UpperLimitRepeatPeriodic = ArgsMsg.t1UpperLimitRepeatPeriodic;
            ArgsMsg.t2LowerLimitRepeatPeriodic = ArgsMsg.t1LowerLimitRepeatPeriodic;

            //区间3
            ArgsMsg.t3UpperLimitCarrierFreq = ArgsMsg.t1UpperLimitCarrierFreq;
            ArgsMsg.t3LowerLimitCarrierFreq = ArgsMsg.t1LowerLimitCarrierFreq;
            ArgsMsg.t3UpperLimitPulseWidth = ArgsMsg.t1UpperLimitPulseWidth;
            ArgsMsg.t3LowerLimitPulseWidth = ArgsMsg.t1LowerLimitPulseWidth;
            ArgsMsg.t3UpperLimitRepeatPeriodic = ArgsMsg.t1UpperLimitRepeatPeriodic;
            ArgsMsg.t3LowerLimitRepeatPeriodic = ArgsMsg.t1LowerLimitRepeatPeriodic;
        }

        if(Interval_num == 1){
            /*
            ArgsMsg.t1UpperLimitCarrierFreq = Short.parseShort(et1FreqUpper.getText().toString());
            if((ArgsMsg.t1UpperLimitCarrierFreq<500) || (ArgsMsg.t1UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t1LowerLimitCarrierFreq = Short.parseShort(et1FreqLower.getText().toString());
            if((ArgsMsg.t1LowerLimitCarrierFreq<500) || (ArgsMsg.t1LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et1PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et1PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1UpperLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et1PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et1PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1LowerLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthLower.getText().toString())*100);

            if ((Float.parseFloat(et1RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et1RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqLower.getText().toString())*5);*/


            ArgsMsg.t2UpperLimitCarrierFreq = Short.parseShort(et2FreqUpper.getText().toString());
            if((ArgsMsg.t2UpperLimitCarrierFreq<500) || (ArgsMsg.t2UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t2LowerLimitCarrierFreq = Short.parseShort(et2FreqLower.getText().toString());
            if((ArgsMsg.t2LowerLimitCarrierFreq<500) || (ArgsMsg.t2LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et2PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et2PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t2UpperLimitPulseWidth = (int)(Float.parseFloat(et2PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et2PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et2PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t2LowerLimitPulseWidth = (int)(Float.parseFloat(et2PulseWidthLower.getText().toString())*100);

            if ((Float.parseFloat(et2RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et2RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t2UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et2RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et2RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et2RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t2LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et2RepeatFreqLower.getText().toString())*5);

            //区间1
            ArgsMsg.t1UpperLimitCarrierFreq = ArgsMsg.t2UpperLimitCarrierFreq;
            ArgsMsg.t1LowerLimitCarrierFreq = ArgsMsg.t2LowerLimitCarrierFreq;
            ArgsMsg.t1UpperLimitPulseWidth = ArgsMsg.t2UpperLimitPulseWidth;
            ArgsMsg.t1LowerLimitPulseWidth = ArgsMsg.t2LowerLimitPulseWidth;
            ArgsMsg.t1UpperLimitRepeatPeriodic = ArgsMsg.t2UpperLimitRepeatPeriodic;
            ArgsMsg.t1LowerLimitRepeatPeriodic = ArgsMsg.t2LowerLimitRepeatPeriodic;

            //区间3
            ArgsMsg.t3UpperLimitCarrierFreq = ArgsMsg.t2UpperLimitCarrierFreq;
            ArgsMsg.t3LowerLimitCarrierFreq = ArgsMsg.t2LowerLimitCarrierFreq;
            ArgsMsg.t3UpperLimitPulseWidth = ArgsMsg.t2UpperLimitPulseWidth;
            ArgsMsg.t3LowerLimitPulseWidth = ArgsMsg.t2LowerLimitPulseWidth;
            ArgsMsg.t3UpperLimitRepeatPeriodic = ArgsMsg.t2UpperLimitRepeatPeriodic;
            ArgsMsg.t3LowerLimitRepeatPeriodic = ArgsMsg.t2LowerLimitRepeatPeriodic;
        }

        if(Interval_num == 2){
            /*
            ArgsMsg.t1UpperLimitCarrierFreq = Short.parseShort(et1FreqUpper.getText().toString());
            if((ArgsMsg.t1UpperLimitCarrierFreq<500) || (ArgsMsg.t1UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t1LowerLimitCarrierFreq = Short.parseShort(et1FreqLower.getText().toString());
            if((ArgsMsg.t1LowerLimitCarrierFreq<500) || (ArgsMsg.t1LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et1PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et1PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1UpperLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et1PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et1PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t1LowerLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthLower.getText().toString())*100);

            if ((Float.parseFloat(et1RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et1RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et1RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t1LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqLower.getText().toString())*5);


            ArgsMsg.t2UpperLimitCarrierFreq = Short.parseShort(et2FreqUpper.getText().toString());
            if((ArgsMsg.t2UpperLimitCarrierFreq<500) || (ArgsMsg.t2UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t2LowerLimitCarrierFreq = Short.parseShort(et2FreqLower.getText().toString());
            if((ArgsMsg.t2LowerLimitCarrierFreq<500) || (ArgsMsg.t2LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et2PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et2PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t2UpperLimitPulseWidth = (int)(Float.parseFloat(et2PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et2PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et2PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t2LowerLimitPulseWidth = (int)(Float.parseFloat(et2PulseWidthLower.getText().toString())*100);

            if ((Float.parseFloat(et2RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et2RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t2UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et2RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et2RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et2RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t2LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et2RepeatFreqLower.getText().toString())*5);*/


            ArgsMsg.t3UpperLimitCarrierFreq = Short.parseShort(et3FreqUpper.getText().toString());
            if((ArgsMsg.t3UpperLimitCarrierFreq<500) || (ArgsMsg.t3UpperLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }
            ArgsMsg.t3LowerLimitCarrierFreq = Short.parseShort(et3FreqLower.getText().toString());
            if((ArgsMsg.t3LowerLimitCarrierFreq<500) || (ArgsMsg.t3LowerLimitCarrierFreq>18000)){
                showTitle("请输入频率范围500-18000");
                return;
            }

            if((Float.parseFloat(et3PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et3PulseWidthUpper.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t3UpperLimitPulseWidth = (int)(Float.parseFloat(et3PulseWidthUpper.getText().toString())*100);

            if((Float.parseFloat(et3PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et3PulseWidthLower.getText().toString())<0.5f)){
                showTitle("请输入脉宽范围0.5-500");
                return;
            }
            ArgsMsg.t3LowerLimitPulseWidth = (int)(Float.parseFloat(et3PulseWidthLower.getText().toString())*100);

            if ((Float.parseFloat(et3RepeatFreqUpper.getText().toString())>6666.7f) || (Float.parseFloat(et3RepeatFreqUpper.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t3UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et3RepeatFreqUpper.getText().toString())*5);

            if ((Float.parseFloat(et3RepeatFreqLower.getText().toString())>6666.7f) || (Float.parseFloat(et3RepeatFreqLower.getText().toString())<33.3f)){
                showTitle("请输入重频范围33.3-6666.7");
                return;
            }
            ArgsMsg.t3LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et3RepeatFreqLower.getText().toString())*5);

            //区间1
            ArgsMsg.t1UpperLimitCarrierFreq = ArgsMsg.t3UpperLimitCarrierFreq;
            ArgsMsg.t1LowerLimitCarrierFreq = ArgsMsg.t3LowerLimitCarrierFreq;
            ArgsMsg.t1UpperLimitPulseWidth = ArgsMsg.t3UpperLimitPulseWidth;
            ArgsMsg.t1LowerLimitPulseWidth = ArgsMsg.t3LowerLimitPulseWidth;
            ArgsMsg.t1UpperLimitRepeatPeriodic = ArgsMsg.t3UpperLimitRepeatPeriodic;
            ArgsMsg.t1LowerLimitRepeatPeriodic = ArgsMsg.t3LowerLimitRepeatPeriodic;

            //区间2
            ArgsMsg.t2UpperLimitCarrierFreq = ArgsMsg.t3UpperLimitCarrierFreq;
            ArgsMsg.t2LowerLimitCarrierFreq = ArgsMsg.t3LowerLimitCarrierFreq;
            ArgsMsg.t2UpperLimitPulseWidth = ArgsMsg.t3UpperLimitPulseWidth;
            ArgsMsg.t2LowerLimitPulseWidth = ArgsMsg.t3LowerLimitPulseWidth;
            ArgsMsg.t2UpperLimitRepeatPeriodic = ArgsMsg.t3UpperLimitRepeatPeriodic;
            ArgsMsg.t2LowerLimitRepeatPeriodic = ArgsMsg.t3LowerLimitRepeatPeriodic;
        }
        /*
        ArgsMsg.t1UpperLimitCarrierFreq = Short.parseShort(et1FreqUpper.getText().toString());
        if((ArgsMsg.t1UpperLimitCarrierFreq<500) || (ArgsMsg.t1UpperLimitCarrierFreq>18000)){
            showTitle("请输入频率范围500-18000");
            return;
        }
        ArgsMsg.t1LowerLimitCarrierFreq = Short.parseShort(et1FreqLower.getText().toString());
        if((ArgsMsg.t1LowerLimitCarrierFreq<500) || (ArgsMsg.t1LowerLimitCarrierFreq>18000)){
            showTitle("请输入频率范围500-18000");
            return;
        }

        if((Float.parseFloat(et1PulseWidthUpper.getText().toString())>500) || (Float.parseFloat(et1PulseWidthUpper.getText().toString())<0.5)){
            showTitle("请输入脉宽范围0.5-500");
            return;
        }
        ArgsMsg.t1UpperLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthUpper.getText().toString())*100);

        if((Float.parseFloat(et1PulseWidthLower.getText().toString())>500) || (Float.parseFloat(et1PulseWidthLower.getText().toString())<0.5)){
            showTitle("请输入脉宽范围0.5-500");
            return;
        }
        ArgsMsg.t1LowerLimitPulseWidth = (int)(Float.parseFloat(et1PulseWidthLower.getText().toString())*100);

        if ((Float.parseFloat(et1RepeatFreqUpper.getText().toString())>6666.7) || (Float.parseFloat(et1RepeatFreqUpper.getText().toString())<33.3)){
            showTitle("请输入重频范围33.3-6666.7");
            return;
        }
        ArgsMsg.t1UpperLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqUpper.getText().toString())*5);

        if ((Float.parseFloat(et1RepeatFreqLower.getText().toString())>6666.7) || (Float.parseFloat(et1RepeatFreqLower.getText().toString())<33.3)){
            showTitle("请输入重频范围33.3-6666.7");
            return;
        }
        ArgsMsg.t1LowerLimitRepeatPeriodic = (int)(Float.parseFloat(et1RepeatFreqLower.getText().toString())*5);

        ArgsMsg.t2UpperLimitCarrierFreq = Short.parseShort(et2FreqUpper.getText().toString());
        ArgsMsg.t2LowerLimitCarrierFreq = Short.parseShort(et2FreqLower.getText().toString());
        ArgsMsg.t2UpperLimitPulseWidth = Short.parseShort(et2PulseWidthUpper.getText().toString());
        ArgsMsg.t2LowerLimitPulseWidth = Short.parseShort(et2PulseWidthLower.getText().toString());
        ArgsMsg.t2UpperLimitRepeatPeriodic = Short.parseShort(et2RepeatFreqUpper.getText().toString());
        ArgsMsg.t2LowerLimitRepeatPeriodic = Short.parseShort(et2RepeatFreqLower.getText().toString());

        ArgsMsg.t3UpperLimitCarrierFreq = Short.parseShort(et3FreqUpper.getText().toString());
        ArgsMsg.t3LowerLimitCarrierFreq = Short.parseShort(et3FreqLower.getText().toString());
        ArgsMsg.t3UpperLimitPulseWidth = Short.parseShort(et3PulseWidthUpper.getText().toString());
        ArgsMsg.t3LowerLimitPulseWidth = Short.parseShort(et3PulseWidthLower.getText().toString());
        ArgsMsg.t3UpperLimitRepeatPeriodic = Short.parseShort(et3RepeatFreqUpper.getText().toString());
        ArgsMsg.t3LowerLimitRepeatPeriodic = Short.parseShort(et3RepeatFreqLower.getText().toString());
        */
        Log.d("MainActivity", "目标1频率上限:"+String.format(" %d",ArgsMsg.t1UpperLimitCarrierFreq));
        Log.d("MainActivity", "目标1频率下限:"+String.format(" %d",ArgsMsg.t1LowerLimitCarrierFreq));
        Log.d("MainActivity", "目标1脉宽上限:"+String.format(" %d",ArgsMsg.t1UpperLimitPulseWidth));
        Log.d("MainActivity", "目标1脉宽下限:"+String.format(" %d",ArgsMsg.t1LowerLimitPulseWidth));
        Log.d("MainActivity", "目标1重频上限:"+String.format(" %d",ArgsMsg.t1UpperLimitRepeatPeriodic));
        Log.d("MainActivity", "目标1重频下限:"+String.format(" %d",ArgsMsg.t1LowerLimitRepeatPeriodic));

        Log.d("MainActivity", "目标2频率上限:"+String.format(" %d",ArgsMsg.t2UpperLimitCarrierFreq));
        Log.d("MainActivity", "目标2频率下限:"+String.format(" %d",ArgsMsg.t2LowerLimitCarrierFreq));
        Log.d("MainActivity", "目标2脉宽上限:"+String.format(" %d",ArgsMsg.t2UpperLimitPulseWidth));
        Log.d("MainActivity", "目标2脉宽下限:"+String.format(" %d",ArgsMsg.t2LowerLimitPulseWidth));
        Log.d("MainActivity", "目标2重频上限:"+String.format(" %d",ArgsMsg.t2UpperLimitRepeatPeriodic));
        Log.d("MainActivity", "目标2重频下限:"+String.format(" %d",ArgsMsg.t2LowerLimitRepeatPeriodic));

        Log.d("MainActivity", "目标3频率上限:"+String.format(" %d",ArgsMsg.t3UpperLimitCarrierFreq));
        Log.d("MainActivity", "目标3频率下限:"+String.format(" %d",ArgsMsg.t3LowerLimitCarrierFreq));
        Log.d("MainActivity", "目标3脉宽上限:"+String.format(" %d",ArgsMsg.t3UpperLimitPulseWidth));
        Log.d("MainActivity", "目标3脉宽下限:"+String.format(" %d",ArgsMsg.t3LowerLimitPulseWidth));
        Log.d("MainActivity", "目标3重频上限:"+String.format(" %d",ArgsMsg.t3UpperLimitRepeatPeriodic));
        Log.d("MainActivity", "目标3重频下限:"+String.format(" %d",ArgsMsg.t3LowerLimitRepeatPeriodic));

        //开始装订和当前时刻
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        f1Tv.setText(date +" 开始装订");

        scoutHost.ShUdpClientSendCmd(ArgsMsg);
    }

    public void sendDevSelfCheck(View view) {
        Log.d("MainActivity", "发送设备自检命令");

        View v = getCurrentFocus();
        if(v!=null){//隐藏键盘
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        byte [] buf = new byte[0x20];
        HostWorkMsg workMsg = new HostWorkMsg(HostWorkCmd.selfCheck,buf);
        scoutHost.ShUdpClientSendCmd(workMsg);
    }

    /*====================================================================================================*/
    /*==============================================页面2=================================================*/
    /*====================================================================================================*/
    HostSearchRspMsg gHostSearchRspMsg;
    int saveCnt = 0;
    private void showHostSearchRspMsgUI(HostSearchRspMsg msg){
        gHostSearchRspMsg = msg;
        String  pollStoreDate="";
        TextView f2Tv = (TextView) findViewById(R.id.f2Tv);
        TextView isLocatedTv = (TextView) findViewById(R.id.isLocatedTv);
        TextView locationNorthInfoTv = (TextView) findViewById(R.id.locationNorthInfoTv);

        TextView f2Freq1 = (TextView) findViewById(R.id.f2Freq1);
        TextView f2PulseWidth1Tv = (TextView) findViewById(R.id.f2PulseWidth1Tv);
        TextView f2RepeatPeriod1Tv = (TextView) findViewById(R.id.f2RepeatPeriod1Tv);
        TextView f2Power1Tv = (TextView) findViewById(R.id.f2Power1Tv);
        TextView f2Direction1Tv = (TextView) findViewById(R.id.f2Direction1Tv);
        TextView f2North1Tv = (TextView) findViewById(R.id.f2North1Tv);

        TextView f2Freq2 = (TextView) findViewById(R.id.f2Freq2);
        TextView f2PulseWidth2Tv = (TextView) findViewById(R.id.f2PulseWidth2Tv);
        TextView f2RepeatPeriod2Tv = (TextView) findViewById(R.id.f2RepeatPeriod2Tv);
        TextView f2Power2Tv = (TextView) findViewById(R.id.f2Power2Tv);
        TextView f2Direction2Tv = (TextView) findViewById(R.id.f2Direction2Tv);
        TextView f2North2Tv = (TextView) findViewById(R.id.f2North2Tv);

        TextView f2Freq3 = (TextView) findViewById(R.id.f2Freq3);
        TextView f2PulseWidth3Tv = (TextView) findViewById(R.id.f2PulseWidth3Tv);
        TextView f2RepeatPeriod3Tv = (TextView) findViewById(R.id.f2RepeatPeriod3Tv);
        TextView f2Power3Tv = (TextView) findViewById(R.id.f2Power3Tv);
        TextView f2Direction3Tv = (TextView) findViewById(R.id.f2Direction3Tv);
        TextView f2North3Tv = (TextView) findViewById(R.id.f2North3Tv);

        String dirAngle = "0";

        String bdStr =

                convertHexToString(Integer.toHexString(msg.getBuf()[90]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[91]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[92]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[93]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[94]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[95]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[96]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[97]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[98]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[99]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[100]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[101]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[102]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[103]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[104]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[105]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[106]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[107]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[108]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[109]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[110]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[111]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[112]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[113]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[114]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[115]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[116]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[117]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[118]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[119]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[120]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[121]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[122]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[123]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[124]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[125]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[126]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[127]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[128]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[129]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[130]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[131]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[132]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[133]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[134]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[135]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[136]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[137]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[138]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[139]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[140]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[141]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[142]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[143]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[144]))+
                convertHexToString(Integer.toHexString(msg.getBuf()[145]));

        Log.d("MainActivity", "北斗信号:"+bdStr);

        if(msg.getResult()== (byte)0xA3){
            Log.d("MainActivity", "正在搜索");
            f2Tv.setText("正在搜索 ");


            if(!(bdStr.equals("")||(bdStr.length()==0))){
                String substring = bdStr.substring(0, bdStr.length() - 1);
                String[] split = substring.split(",");//以逗号分割
                if(split.length>=9){
                    try{
                        Log.d("MainActivity", "北斗信号分隔后长度:"+split.length);

                        String sIsLocatedTv = "";
                        if(split[0].equals("A")){
                            sIsLocatedTv +="已定位";
                        }else if(split[0].equals("V")){
                            sIsLocatedTv +="未定位";
                        }

                        if(split[1].equals("A")){
                            sIsLocatedTv +="已定向";
                        }else if(split[1].equals("V")){
                            sIsLocatedTv +="未定向";
                        }
                        isLocatedTv.setText(sIsLocatedTv);


                        String sjd = "";
                        if(split[3].equals("E")){
                            sjd+="东经";
                        }else if(split[3].equals("W")){
                            sjd+="西经";
                        }
                        sjd+=strCovJwd(split[2]);
                        //Log.d("MainActivity", "经度:"+sjd);

                        String swd = "";
                        if(split[5].equals("N")){
                            sjd+="北纬";
                        }else if(split[5].equals("S")){
                            sjd+="南纬";
                        }
                        swd+=strCovJwd(split[4]);
                        //Log.d("MainActivity", "纬度:"+swd);
                        //Log.d("MainActivity", "卫星数:"+split[6]);


                        float fDirAngle;
                        fDirAngle = Float.valueOf(split[8])-90+1.5537f;
                        DecimalFormat decimalFormat=new DecimalFormat(".0");
                        String sDirAngle=decimalFormat.format(fDirAngle);
                        fDirAngle = Float.valueOf(sDirAngle);
                        if(fDirAngle<0){
                            fDirAngle += 360;
                        }
                        dirAngle = String.valueOf(fDirAngle);
                        //Log.d("MainActivity", "方位角:"+dirAngle);

                        if(split[6].equals("0.0000")){
                            locationNorthInfoTv.setText(sjd+swd+"指北N/A");
                        }else{
                            locationNorthInfoTv.setText(sjd+swd+"指北"+dirAngle);
                        }

                    }catch(Exception e){

                    }

                }

            }

            /*
            if((msg.getBuf()[144]&0xff)==0){
                //未定位未指北
                isLocatedTv.setText("未定位未指北");
            }else if((msg.getBuf()[144]&0xff)==1){
                //已定位未指北
                isLocatedTv.setText("已定位未指北");
            }else if((msg.getBuf()[144]&0xff)==16){
                //已指北未定位
                isLocatedTv.setText("已指北未定位");
            }else if((msg.getBuf()[144]&0xff)==17){
                //已定位已指北
                isLocatedTv.setText("已定位已指北");
            }

            String longitude = String.format(" %.2f",(float)(((msg.getBuf()[149]&0xff) + (((msg.getBuf()[150]&0xff) << 8)&0xff00))*0.01));
            String latitude = String.format(" %.2f",(float)(((msg.getBuf()[151]&0xff) + (((msg.getBuf()[152]&0xff) << 8)&0xff00))*0.01));
            Log.d("MainActivity", "经度:"+ longitude);
            Log.d("MainActivity", "纬度:"+ latitude);
            locationNorthInfoTv.setText(longitude+"°"+","+latitude+ "°");*/


            //Log.d("MainActivity", "目标1序号:"+String.valueOf(msg.getBuf()[113]&0xff));
            if((msg.getBuf()[113]&0xff)!=0){

            }
            if((((msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00)) <= 18000)){
                String freq1 = String.format(" %d",(msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00));
                f2Freq1.setText(freq1);
                String pulseWidth1 = String.format(" %.0f",(float)(((msg.getBuf()[18]&0xff) + (((msg.getBuf()[19]&0xff) << 8)&0xff00))*0.01));
                f2PulseWidth1Tv.setText(pulseWidth1);
                String repeatPeriod1 = String.format(" %.0f",(float)(((msg.getBuf()[16]&0xff) + (((msg.getBuf()[17]&0xff) << 8)&0xff00))*0.2));
                f2RepeatPeriod1Tv.setText(repeatPeriod1);

                String Power1="";
                if((msg.getBuf()[20]&0xff)<100){
                    Power1 = String.format(" %d",(msg.getBuf()[20]&0xff));
                    f2Power1Tv.setText("-"+Power1);
                }else{
                    Power1 = String.format(" %d",((msg.getBuf()[20]&0xff)-100));
                    f2Power1Tv.setText(Power1);
                }

                //String Power1 = String.format(" %d",(msg.getBuf()[20]&0xff));
                //f2Power1Tv.setText(Power1);

                //String Direction1 = String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff) + (((msg.getBuf()[13]&0xff) << 8)&0xff00))*0.002));
                String Direction1 = "";
                if((msg.getBuf()[13]&0x80)==0x00){
                    Direction1 = String.format(" %.1f",(float)((((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[13]&0x80)==0x80){
                    Direction1 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction1Tv.setText(Direction1);
                //String North1 = String.format(" %.1f",(float)(((msg.getBuf()[155]&0xff) + (((msg.getBuf()[156]&0xff) << 8)&0xff00))*0.01));
                f2North1Tv.setText(dirAngle);

                //pollStoreDate += "序号1 频率:"+freq1+" 脉宽:"+pulseWidth1+" 重频:"+repeatPeriod1+" 功率:"+Power1+" 方位:"+Direction1+" 指北:"+North1+"\r\n";

            }else{
                f2Freq1.setText("0");
                f2PulseWidth1Tv.setText("0");
                f2RepeatPeriod1Tv.setText("0");
                f2Power1Tv.setText("0");
                f2Direction1Tv.setText("0");
                f2North1Tv.setText("0");
            }

            //Log.d("MainActivity", "目标2序号:"+String.valueOf(msg.getBuf()[114]&0xff));
            if((msg.getBuf()[114]&0xff)!=0){

            }
            if((((msg.getBuf()[25]&0xff) + (((msg.getBuf()[26]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[25]&0xff) + (((msg.getBuf()[26]&0xff) << 8)&0xff00)) <= 18000)) {
                String freq2 = String.format(" %d", (msg.getBuf()[25] & 0xff) + (((msg.getBuf()[26] & 0xff) << 8) & 0xff00));
                f2Freq2.setText(freq2);
                String pulseWidth2 = String.format(" %.0f", (float) (((msg.getBuf()[29] & 0xff) + (((msg.getBuf()[30] & 0xff) << 8) & 0xff00)) * 0.01));
                f2PulseWidth2Tv.setText(pulseWidth2);
                String repeatPeriod2 = String.format(" %.0f", (float) (((msg.getBuf()[27] & 0xff) + (((msg.getBuf()[28] & 0xff) << 8) & 0xff00)) * 0.2));
                f2RepeatPeriod2Tv.setText(repeatPeriod2);
                String Power2="";
                if((msg.getBuf()[31]&0xff)<100){
                    Power2 = String.format(" %d",(msg.getBuf()[31]&0xff));
                    f2Power2Tv.setText("-"+Power2);
                }else{
                    Power2 = String.format(" %d",((msg.getBuf()[31]&0xff)-100));
                    f2Power2Tv.setText(Power2);
                }

                //String Power2 = String.format(" %d", (msg.getBuf()[31] & 0xff));
                //f2Power2Tv.setText(Power2);

                //String Direction2 = String.format(" %.1f", (float) (((msg.getBuf()[23] & 0xff) + (((msg.getBuf()[24] & 0xff) << 8) & 0xff00)) * 0.002));
                String Direction2 = "";
                if((msg.getBuf()[24]&0x80)==0x00){
                    Direction2 = String.format(" %.1f",(float)((((msg.getBuf()[23]&0xff)+((msg.getBuf()[24]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[24]&0x80)==0x80){
                    Direction2 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[23]&0xff)+((msg.getBuf()[24]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction2Tv.setText(Direction2);
                //String North2 = String.format(" %.1f", (float) (((msg.getBuf()[155] & 0xff) + (((msg.getBuf()[156] & 0xff) << 8) & 0xff00)) * 0.01));
                f2North2Tv.setText(dirAngle);

               // pollStoreDate += "序号2 频率:"+freq2+" 脉宽:"+pulseWidth2+" 重频:"+repeatPeriod2+" 功率:"+Power2+" 方位:"+Direction2+" 指北:"+North2+"\r\n";
            }else{
                f2Freq2.setText("0");
                f2PulseWidth2Tv.setText("0");
                f2RepeatPeriod2Tv.setText("0");
                f2Power2Tv.setText("0");
                f2Direction2Tv.setText("0");
                f2North2Tv.setText("0");
            }

            //Log.d("MainActivity", "目标3序号:"+String.valueOf(msg.getBuf()[115]&0xff));
            if((msg.getBuf()[115]&0xff)!=0){

            }
            if((((msg.getBuf()[36]&0xff) + (((msg.getBuf()[37]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[36]&0xff) + (((msg.getBuf()[37]&0xff) << 8)&0xff00)) <= 18000)) {

                String freq3 = String.format(" %d", (msg.getBuf()[36] & 0xff) + (((msg.getBuf()[37] & 0xff) << 8) & 0xff00));
                f2Freq3.setText(freq3);
                String pulseWidth3 = String.format(" %.0f", (float) (((msg.getBuf()[40] & 0xff) + (((msg.getBuf()[41] & 0xff) << 8) & 0xff00)) * 0.01));
                f2PulseWidth3Tv.setText(pulseWidth3);
                String repeatPeriod3 = String.format(" %.0f", (float) (((msg.getBuf()[38] & 0xff) + (((msg.getBuf()[39] & 0xff) << 8) & 0xff00)) * 0.2));
                f2RepeatPeriod3Tv.setText(repeatPeriod3);
                String Power3="";
                if((msg.getBuf()[42]&0xff)<100){
                    Power3 = String.format(" %d",(msg.getBuf()[42]&0xff));
                    f2Power3Tv.setText("-"+Power3);
                }else{
                    Power3 = String.format(" %d",((msg.getBuf()[42]&0xff)-100));
                    f2Power3Tv.setText(Power3);
                }

                //String Power3 = String.format(" %d", (msg.getBuf()[42] & 0xff));
                //f2Power3Tv.setText(Power3);
                //String Direction3 = String.format(" %.1f", (float) (((msg.getBuf()[34] & 0xff) + (((msg.getBuf()[35] & 0xff) << 8) & 0xff00)) * 0.002));
                String Direction3 = "";
                if((msg.getBuf()[35]&0x80)==0x00){
                    Direction3 = String.format(" %.1f",(float)((((msg.getBuf()[34]&0xff)+((msg.getBuf()[35]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[35]&0x80)==0x80){
                    Direction3 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[34]&0xff)+((msg.getBuf()[35]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction3Tv.setText(Direction3);
                //String North3 = String.format(" %.1f", (float) (((msg.getBuf()[155] & 0xff) + (((msg.getBuf()[156] & 0xff) << 8) & 0xff00)) * 0.01));
                f2North3Tv.setText(dirAngle);
                //pollStoreDate += "序号3 频率:"+freq3+" 脉宽:"+pulseWidth3+" 重频:"+repeatPeriod3+" 功率:"+Power3+" 方位:"+Direction3+" 指北:"+North3+"\r\n";
            }else{
                f2Freq3.setText("0");
                f2PulseWidth3Tv.setText("0");
                f2RepeatPeriod3Tv.setText("0");
                f2Power3Tv.setText("0");
                f2Direction3Tv.setText("0");
                f2North3Tv.setText("0");
            }

        }else if(msg.getResult() == (byte)0xB3){

            //Log.d("MainActivity", "跟踪");
            f2Tv.setText("跟踪 ");
            String dwdxjwd = "";
            if(!(bdStr.equals("")||(bdStr.length()==0))){
                String substring = bdStr.substring(0, bdStr.length() - 1);
                String[] split = substring.split(",");//以逗号分割
                if(split.length>=9){
                    try{
                        Log.d("MainActivity", "北斗信号分隔后长度:"+split.length);

                        String sIsLocatedTv = "";
                        if(split[0].equals("A")){
                            sIsLocatedTv +="已定位";
                        }else if(split[0].equals("V")){
                            sIsLocatedTv +="未定位";
                        }

                        if(split[1].equals("A")){
                            sIsLocatedTv +="已定向";
                        }else if(split[1].equals("V")){
                            sIsLocatedTv +="未定向";
                        }
                        isLocatedTv.setText(sIsLocatedTv);


                        String sjd = "";
                        if(split[3].equals("E")){
                            sjd+="东经";
                        }else if(split[3].equals("W")){
                            sjd+="西经";
                        }
                        sjd+=strCovJwd(split[2]);
                        //Log.d("MainActivity", "经度:"+sjd);

                        String swd = "";
                        if(split[5].equals("N")){
                            sjd+="北纬";
                        }else if(split[5].equals("S")){
                            sjd+="南纬";
                        }
                        swd+=strCovJwd(split[4]);
                        //Log.d("MainActivity", "纬度:"+swd);
                        //Log.d("MainActivity", "卫星数:"+split[6]);
                        /*
                        if(split[6].equals("0.0000")){
                            locationNorthInfoTv.setText(sjd+swd+"卫星数0");
                        }else{
                            locationNorthInfoTv.setText(sjd+swd+"卫星数"+split[6]);
                        }*/

                        float fDirAngle;
                        fDirAngle = Float.valueOf(split[8])-90+1.5537f;

                        DecimalFormat decimalFormat=new DecimalFormat(".0");
                        String sDirAngle=decimalFormat.format(fDirAngle);
                        fDirAngle = Float.valueOf(sDirAngle);

                        if(fDirAngle<0){
                            fDirAngle += 360;
                        }
                        dirAngle = String.valueOf(fDirAngle);
                        //Log.d("MainActivity", "方位角:"+dirAngle);
                        if(split[6].equals("0.0000")){
                            locationNorthInfoTv.setText(sjd+swd+"指北N/A");
                        }else{
                            locationNorthInfoTv.setText(sjd+swd+"指北"+dirAngle);
                        }
                        dwdxjwd = sIsLocatedTv+sjd+swd;

                    }catch(Exception e){

                    }

                }

            }

        /*
        if((msg.getBuf()[144]&0xff)==0){
        //未定位未指北
            isLocatedTv.setText("未定位未指北");
        }else if((msg.getBuf()[144]&0xff)==1){
        //已定位未指北
            isLocatedTv.setText("已定位未指北");
        }else if((msg.getBuf()[144]&0xff)==16){
        //已指北未定位
            isLocatedTv.setText("已指北未定位");
        }else if((msg.getBuf()[144]&0xff)==17){
        //已定位已指北
            isLocatedTv.setText("已定位已指北");
        }

        //定位时间
        if( ((msg.getBuf()[145]&0xff)>=0) && ((msg.getBuf()[145]&0xff)<=23) &&
                ((msg.getBuf()[146]&0xff)>=0) && ((msg.getBuf()[146]&0xff)<=59) &&
                ((msg.getBuf()[147]&0xff)>=0) && ((msg.getBuf()[147]&0xff)<=59) &&
                ((msg.getBuf()[148]&0xff)>=0) && ((msg.getBuf()[148]&0xff)<=99) ){
            String locatedHour = String.format("%d",(short)(msg.getBuf()[145]&0xff));
            String locatedMinute = String.format("%d",(short)(msg.getBuf()[146]&0xff));
            String locatedSecond = String.format("%d",(short)(msg.getBuf()[147]&0xff));
            String locatedMillisecond = String.format("%d",(short)(msg.getBuf()[148]&0xff));

            String locatedTime = locatedHour+":"+locatedMinute+":"+locatedSecond+"."+locatedMillisecond;
            Log.d("MainActivity", "定位时间:"+ locatedTime);
        }else{
            Log.d("MainActivity", "定位时间数据异常!");
        }

        //Log.d("MainActivity", "16进制转换字符测试："+convertHexToString(Integer.toHexString(msg.getBuf()[0])));

        String longitude = String.format(" %.2f",(float)(((msg.getBuf()[149]&0xff) + (((msg.getBuf()[150]&0xff) << 8)&0xff00))*0.01));
        String latitude = String.format(" %.2f",(float)(((msg.getBuf()[151]&0xff) + (((msg.getBuf()[152]&0xff) << 8)&0xff00))*0.01));
        Log.d("MainActivity", "经度:"+ longitude);
        Log.d("MainActivity", "纬度:"+ latitude);
        locationNorthInfoTv.setText(longitude+"°"+","+latitude+ "°");*/


            //Log.d("MainActivity", "目标1序号:"+String.valueOf(msg.getBuf()[113]&0xff));
            if((msg.getBuf()[113]&0xff)!=0){

            }
            if((((msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00)) <= 18000)){
                String freq1 = String.format(" %d",(msg.getBuf()[14]&0xff) + (((msg.getBuf()[15]&0xff) << 8)&0xff00));
                f2Freq1.setText(freq1);
                String pulseWidth1 = String.format(" %.0f",(float)(((msg.getBuf()[18]&0xff) + (((msg.getBuf()[19]&0xff) << 8)&0xff00))*0.01));
                f2PulseWidth1Tv.setText(pulseWidth1);
                String repeatPeriod1 = String.format(" %.0f",(float)(((msg.getBuf()[16]&0xff) + (((msg.getBuf()[17]&0xff) << 8)&0xff00))*0.2));
                f2RepeatPeriod1Tv.setText(repeatPeriod1);

                String Power1 = "";
                if((msg.getBuf()[20]&0xff)<100){
                    Power1 = String.format(" %d",(msg.getBuf()[20]&0xff));
                    f2Power1Tv.setText("-"+Power1);
                }else{
                    Power1 = String.format(" %d",((msg.getBuf()[20]&0xff)-100));
                    f2Power1Tv.setText(Power1);
                }

                //String Power1 = String.format(" %d",(msg.getBuf()[20]&0xff));
                //f2Power1Tv.setText(Power1);

                //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                //String.valueOf((msg.getBuf()[12]+(msg.getBuf()[12]<<8))&0x7fff);

                //String Direction1 = String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff) + (((msg.getBuf()[13]&0xff) << 8)&0xff00))*0.002));
                String Direction1 = "";
                if((msg.getBuf()[13]&0x80)==0x00){
                    Direction1 = String.format(" %.1f",(float)((((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[13]&0x80)==0x80){
                    Direction1 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction1Tv.setText(Direction1);
                //String North1 = String.format(" %.1f",(float)(((msg.getBuf()[155]&0xff) + (((msg.getBuf()[156]&0xff) << 8)&0xff00))*0.01));
                f2North1Tv.setText(dirAngle);
                pollStoreDate += "序号1 频率:"+freq1+" 脉宽:"+pulseWidth1+" 重频:"+repeatPeriod1+" 功率:"+Power1+" 方位:"+Direction1+" 指北:"+dirAngle+dwdxjwd+"\r\n";
            }else{
                f2Freq1.setText("0");
                f2PulseWidth1Tv.setText("0");
                f2RepeatPeriod1Tv.setText("0");
                f2Power1Tv.setText("0");
                f2Direction1Tv.setText("0");
                f2North1Tv.setText("0");
            }

            //Log.d("MainActivity", "目标2序号:"+String.valueOf(msg.getBuf()[114]&0xff));
            if((msg.getBuf()[114]&0xff)!=0){

            }
            if((((msg.getBuf()[25]&0xff) + (((msg.getBuf()[26]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[25]&0xff) + (((msg.getBuf()[26]&0xff) << 8)&0xff00)) <= 18000)) {
                String freq2 = String.format(" %d", (msg.getBuf()[25] & 0xff) + (((msg.getBuf()[26] & 0xff) << 8) & 0xff00));
                f2Freq2.setText(freq2);
                String pulseWidth2 = String.format(" %.0f", (float) (((msg.getBuf()[29] & 0xff) + (((msg.getBuf()[30] & 0xff) << 8) & 0xff00)) * 0.01));
                f2PulseWidth2Tv.setText(pulseWidth2);
                String repeatPeriod2 = String.format(" %.0f", (float) (((msg.getBuf()[27] & 0xff) + (((msg.getBuf()[28] & 0xff) << 8) & 0xff00)) * 0.2));
                f2RepeatPeriod2Tv.setText(repeatPeriod2);
                String Power2 = "";
                if((msg.getBuf()[31]&0xff)<100){
                    Power2 = String.format(" %d",(msg.getBuf()[31]&0xff));
                    f2Power2Tv.setText("-"+Power2);
                }else{
                    Power2 = String.format(" %d",((msg.getBuf()[31]&0xff)-100));
                    f2Power2Tv.setText(Power2);
                }

                //String Power2 = String.format(" %d", (msg.getBuf()[31] & 0xff));
                //f2Power2Tv.setText(Power2);

                //String Direction2 = String.format(" %.1f", (float) (((msg.getBuf()[23] & 0xff) + (((msg.getBuf()[24] & 0xff) << 8) & 0xff00)) * 0.002));
                String Direction2 = "";
                if((msg.getBuf()[24]&0x80)==0x00){
                    Direction2 = String.format(" %.1f",(float)((((msg.getBuf()[23]&0xff)+((msg.getBuf()[24]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[24]&0x80)==0x80){
                    Direction2 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[23]&0xff)+((msg.getBuf()[24]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction2Tv.setText(Direction2);
                //String North2 = String.format(" %.1f", (float) (((msg.getBuf()[155] & 0xff) + (((msg.getBuf()[156] & 0xff) << 8) & 0xff00)) * 0.01));
                f2North2Tv.setText(dirAngle);
                pollStoreDate += "序号2 频率:"+freq2+" 脉宽:"+pulseWidth2+" 重频:"+repeatPeriod2+" 功率:"+Power2+" 方位:"+Direction2+" 指北:"+dirAngle+dwdxjwd+"\r\n";
            }else{
                f2Freq2.setText("0");
                f2PulseWidth2Tv.setText("0");
                f2RepeatPeriod2Tv.setText("0");
                f2Power2Tv.setText("0");
                f2Direction2Tv.setText("0");
                f2North2Tv.setText("0");
            }

            //Log.d("MainActivity", "目标3序号:"+String.valueOf(msg.getBuf()[115]&0xff));
            if((msg.getBuf()[115]&0xff)!=0){

            }
            if((((msg.getBuf()[36]&0xff) + (((msg.getBuf()[37]&0xff) << 8)&0xff00))>= 500) && (((msg.getBuf()[36]&0xff) + (((msg.getBuf()[37]&0xff) << 8)&0xff00)) <= 18000)) {

                String freq3 = String.format(" %d", (msg.getBuf()[36] & 0xff) + (((msg.getBuf()[37] & 0xff) << 8) & 0xff00));
                f2Freq3.setText(freq3);
                String pulseWidth3 = String.format(" %.0f", (float) (((msg.getBuf()[40] & 0xff) + (((msg.getBuf()[41] & 0xff) << 8) & 0xff00)) * 0.01));
                f2PulseWidth3Tv.setText(pulseWidth3);
                String repeatPeriod3 = String.format(" %.0f", (float) (((msg.getBuf()[38] & 0xff) + (((msg.getBuf()[39] & 0xff) << 8) & 0xff00)) * 0.2));
                f2RepeatPeriod3Tv.setText(repeatPeriod3);
                String Power3 = "";
                if((msg.getBuf()[42]&0xff)<100){
                    Power3 = String.format(" %d",(msg.getBuf()[42]&0xff));
                    f2Power3Tv.setText("-"+Power3);
                }else{
                    Power3 = String.format(" %d",((msg.getBuf()[42]&0xff)-100));
                    f2Power3Tv.setText(Power3);
                }

                //String Power3 = String.format(" %d", (msg.getBuf()[42] & 0xff));
                //f2Power3Tv.setText(Power3);
                //String Direction3 = String.format(" %.1f", (float) (((msg.getBuf()[34] & 0xff) + (((msg.getBuf()[35] & 0xff) << 8) & 0xff00)) * 0.002));
                String Direction3 = "";
                if((msg.getBuf()[35]&0x80)==0x00){
                    Direction3 = String.format(" %.1f",(float)((((msg.getBuf()[34]&0xff)+((msg.getBuf()[35]<<8)&0xff00))&0x7fff)*0.002));
                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }else if((msg.getBuf()[35]&0x80)==0x80){
                    Direction3 = "-"+String.format(" %.1f",(float)((32768-(((msg.getBuf()[34]&0xff)+((msg.getBuf()[35]<<8)&0xff00))&0x7fff))*0.002));
                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                }
                f2Direction3Tv.setText(Direction3);
                //String North3 = String.format(" %.1f", (float) (((msg.getBuf()[155] & 0xff) + (((msg.getBuf()[156] & 0xff) << 8) & 0xff00)) * 0.01));
                f2North3Tv.setText(dirAngle);
                pollStoreDate += "序号3 频率:"+freq3+" 脉宽:"+pulseWidth3+" 重频:"+repeatPeriod3+" 功率:"+Power3+" 方位:"+Direction3+" 指北:"+dirAngle+dwdxjwd+"\r\n";
            }else{
                f2Freq3.setText("0");
                f2PulseWidth3Tv.setText("0");
                f2RepeatPeriod3Tv.setText("0");
                f2Power3Tv.setText("0");
                f2Direction3Tv.setText("0");
                f2North3Tv.setText("0");
            }


            saveCnt++;
            //saveCnt=5;
            if(saveCnt>=5){
                saveCnt=0;
                //满足条件存储
                //Log.d("MainActivity", "准备存储");
                LogUtil.d(pollStoreDate);

            }

        }else if(msg.getResult() == (byte)0xF3){
            Log.d("MainActivity", "丢失");
            f2Tv.setText("丢失");
        }else{
            Log.d("MainActivity", "未知结果");
            f2Tv.setText("未知结果");
        }
    }

    private void showHostRealseSearchRspMsgUI(HostRealseSearchRspMsg msg){
        if(msg.getResult()== releaseSearchSuccess){
            TextView f2Tv = (TextView) findViewById(R.id.f2Tv);
            Log.d("MainActivity", "放弃搜索成功");
            f2Tv.setText("放弃搜索成功");
        }
    }


    public void sendStartSearch(View view){
        Log.d("MainActivity", "发送启动搜索命令");
        byte [] buf = new byte[0x20];
        HostWorkMsg workMsg = new HostWorkMsg(HostWorkCmd.search,buf);
        scoutHost.ShUdpClientSendCmd(workMsg);
    }

    public void sendStopSearch(View view){
        Log.d("MainActivity", "发送停止搜索命令");
        byte [] buf = new byte[0x20];
        HostWorkMsg workMsg = new HostWorkMsg(releaseSearch,buf);
        scoutHost.ShUdpClientSendCmd(workMsg);
    }

    public void clearF2Tv(View view) {
        Log.d("MainActivity", "清除文本框");
        TextView f2Tv = (TextView) findViewById(R.id.f2Tv);
        f2Tv.setText("");
    }

    public void saveProbeData(View view) {
        Log.d("MainActivity", "存储选中的探测参数");

        TextView f2Tv = (TextView) findViewById(R.id.f2Tv);

        TextView f2Freq1 = (TextView) findViewById(R.id.f2Freq1);
        TextView f2PulseWidth1Tv = (TextView) findViewById(R.id.f2PulseWidth1Tv);
        TextView f2RepeatPeriod1Tv = (TextView) findViewById(R.id.f2RepeatPeriod1Tv);
        TextView f2Power1Tv = (TextView) findViewById(R.id.f2Power1Tv);
        TextView f2Direction1Tv = (TextView) findViewById(R.id.f2Direction1Tv);
        TextView f2North1Tv = (TextView) findViewById(R.id.f2North1Tv);

        TextView f2Freq2 = (TextView) findViewById(R.id.f2Freq2);
        TextView f2PulseWidth2Tv = (TextView) findViewById(R.id.f2PulseWidth2Tv);
        TextView f2RepeatPeriod2Tv = (TextView) findViewById(R.id.f2RepeatPeriod2Tv);
        TextView f2Power2Tv = (TextView) findViewById(R.id.f2Power2Tv);
        TextView f2Direction2Tv = (TextView) findViewById(R.id.f2Direction2Tv);
        TextView f2North2Tv = (TextView) findViewById(R.id.f2North2Tv);


        TextView f2Freq3 = (TextView) findViewById(R.id.f2Freq3);
        TextView f2PulseWidth3Tv = (TextView) findViewById(R.id.f2PulseWidth3Tv);
        TextView f2RepeatPeriod3Tv = (TextView) findViewById(R.id.f2RepeatPeriod3Tv);
        TextView f2Power3Tv = (TextView) findViewById(R.id.f2Power3Tv);
        TextView f2Direction3Tv = (TextView) findViewById(R.id.f2Direction3Tv);
        TextView f2North3Tv = (TextView) findViewById(R.id.f2North3Tv);

        Log.d("MainActivity", f2Direction1Tv.getText().toString());
        byte [] buf = new byte[33];
        String sStore = "";
        int i = 0;
        Boolean isValidData = false;
        //if((f2Freq1.getCurrentTextColor() == Color.RED) || (f2Freq2.getCurrentTextColor() == Color.RED) || (f2Freq3.getCurrentTextColor() == Color.RED)){
            //if(f2Freq1.getCurrentTextColor() == Color.RED){
        if(gHostSearchRspMsg == null){
            Toast.makeText(this, getString(R.string.is_valid_data),Toast.LENGTH_SHORT).show();
            return;
        }
        //全为0跳过存储
        if((gHostSearchRspMsg.getBuf()[14]!=0)||(gHostSearchRspMsg.getBuf()[15]!=0)||(gHostSearchRspMsg.getBuf()[18]!=0)
                ||(gHostSearchRspMsg.getBuf()[19]!=0)||(gHostSearchRspMsg.getBuf()[16]!=0)||(gHostSearchRspMsg.getBuf()[17]!=0)
                ||(gHostSearchRspMsg.getBuf()[20]!=0)||(gHostSearchRspMsg.getBuf()[12]!=0)||(gHostSearchRspMsg.getBuf()[13]!=0)){
                isValidData = true;
                buf[i] = gHostSearchRspMsg.getBuf()[14];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[15];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[18];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[19];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[16];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[17];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[20];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[12];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[13];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[155];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[156];
                i++;

                sStore += f2Freq1.getText().toString()+","+f2PulseWidth1Tv.getText().toString()+","+
                        f2RepeatPeriod1Tv.getText().toString()+","+f2Power1Tv.getText().toString()+","+
                        f2Direction1Tv.getText().toString()+","+f2North1Tv.getText().toString()+",";
            }

        if((gHostSearchRspMsg.getBuf()[25]!=0)||(gHostSearchRspMsg.getBuf()[26]!=0)||(gHostSearchRspMsg.getBuf()[29]!=0)
                ||(gHostSearchRspMsg.getBuf()[30]!=0)||(gHostSearchRspMsg.getBuf()[27]!=0)||(gHostSearchRspMsg.getBuf()[28]!=0)
                ||(gHostSearchRspMsg.getBuf()[31]!=0)||(gHostSearchRspMsg.getBuf()[23]!=0)||(gHostSearchRspMsg.getBuf()[24]!=0)){
                isValidData = true;
                buf[i] = gHostSearchRspMsg.getBuf()[25];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[26];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[29];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[30];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[27];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[28];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[31];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[23];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[24];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[155];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[156];
                i++;

            sStore += f2Freq2.getText().toString()+","+f2PulseWidth2Tv.getText().toString()+","+
                    f2RepeatPeriod2Tv.getText().toString()+","+f2Power2Tv.getText().toString()+","+
                    f2Direction2Tv.getText().toString()+","+f2North2Tv.getText().toString()+",";
            }

        if((gHostSearchRspMsg.getBuf()[36]!=0)||(gHostSearchRspMsg.getBuf()[37]!=0)||(gHostSearchRspMsg.getBuf()[40]!=0)
                ||(gHostSearchRspMsg.getBuf()[41]!=0)||(gHostSearchRspMsg.getBuf()[41]!=0)||(gHostSearchRspMsg.getBuf()[38]!=0)
                ||(gHostSearchRspMsg.getBuf()[39]!=0)||(gHostSearchRspMsg.getBuf()[42]!=0)||(gHostSearchRspMsg.getBuf()[34]!=0)){
                isValidData = true;
                buf[i] = gHostSearchRspMsg.getBuf()[36];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[37];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[40];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[41];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[38];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[39];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[42];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[34];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[35];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[155];
                i++;
                buf[i] = gHostSearchRspMsg.getBuf()[156];
            sStore += f2Freq3.getText().toString()+","+f2PulseWidth3Tv.getText().toString()+","+
                    f2RepeatPeriod3Tv.getText().toString()+","+f2Power3Tv.getText().toString()+","+
                    f2Direction3Tv.getText().toString()+","+f2North3Tv.getText().toString()+",";
            }


            /*

            buf[0] = (byte)((Short.parseShort(f2Freq1.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[1] = (byte)((Short.parseShort(f2Freq1.getText().toString().replaceAll("\\s*","")))>>8);

            buf[2] = (byte)((Short.parseShort(f2PulseWidth1Tv.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[3] = (byte)((Short.parseShort(f2PulseWidth1Tv.getText().toString().replaceAll("\\s*","")))>>8);

            buf[4] = (byte)((Short.parseShort(f2RepeatPeriod1Tv.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[5] = (byte)((Short.parseShort(f2RepeatPeriod1Tv.getText().toString().replaceAll("\\s*","")))>>8);

            buf[6] = (byte)((Short.parseShort(f2Power1Tv.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[7] = (byte)((Short.parseShort(f2Power1Tv.getText().toString().replaceAll("\\s*","")))>>8);

            buf[8] = (byte)((Integer.parseInt(f2Direction1Tv.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[9] = (byte)((Integer.parseInt(f2Direction1Tv.getText().toString().replaceAll("\\s*","")))>>8);

            buf[10] = (byte)((Short.parseShort(f2North1Tv.getText().toString().replaceAll("\\s*","")))&0x00ff);
            buf[11] = (byte)((Short.parseShort(f2North1Tv.getText().toString().replaceAll("\\s*","")))>>8);
            */
            if(isValidData==true){
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
                Context content =getApplicationContext();
                //saveFileToSDCardPrivateFilesDir(buf,"local",date,content);

                f2Tv.setText("存储文件名:"+date);
                Toast.makeText(this, getString(R.string.save_success),Toast.LENGTH_SHORT).show();

                //String s = getSDCardPrivateFilesDir(content,"local");
                //Log.d("MainActivity", "读取文件路径:"+s);


                String pathName = "/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/manual/"+date;
                string2File(sStore,pathName);
                /*
                File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/manual");
                String sRead = file2String(file,"utf8");
                Log.d("MainActivity", "测试读取文件中的字符串:"+sRead);*/


            }else{
                Toast.makeText(this, getString(R.string.is_valid_data),Toast.LENGTH_SHORT).show();
            }

            /*
            String strBuf = "";
            for(int i=0;i<2;i++){
                strBuf += String.format(" %02x",buf[i]);
            }
            Log.d("MainActivity", "存储报文内容:"+strBuf);//16进制字符串
            Log.d("MainActivity", "存储string报文内容:"+f2Freq1.getText().toString());//整型字符串
            */
        //}else{
            //Toast.makeText(this, getString(R.string.select_save_data),Toast.LENGTH_SHORT).show();
            //return;
        //}
    }


    public void remoteConnect(View view){
        Log.d("MainActivity", "远程连接");

    }

    public void remoteReceive(View view){
        Log.d("MainActivity", "远程接收");

    }

    public void remoteSend(View view){
        Log.d("MainActivity", "远程发送");

    }

    public void f2LocalFileSelect(View view){
        Log.d("MainActivity", "本地文件选择");
        File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/local");
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(file), "*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 1);
    }


    public void f2SelectLine1(View view) {
        /*
        Log.d("MainActivity", "选中F2第1行");
        TextView f2Freq1 = (TextView) findViewById(R.id.f2Freq1);
        TextView f2PulseWidth1Tv = (TextView) findViewById(R.id.f2PulseWidth1Tv);
        TextView f2RepeatPeriod1Tv = (TextView) findViewById(R.id.f2RepeatPeriod1Tv);
        TextView f2Power1Tv = (TextView) findViewById(R.id.f2Power1Tv);
        TextView f2Direction1Tv = (TextView) findViewById(R.id.f2Direction1Tv);
        TextView f2North1Tv = (TextView) findViewById(R.id.f2North1Tv);

        if(f2Freq1.getCurrentTextColor() == Color.RED){
            f2Freq1.setTextColor(Color.BLACK);
            f2PulseWidth1Tv.setTextColor(Color.BLACK);
            f2RepeatPeriod1Tv.setTextColor(Color.BLACK);
            f2Power1Tv.setTextColor(Color.BLACK);
            f2Direction1Tv.setTextColor(Color.BLACK);
            f2North1Tv.setTextColor(Color.BLACK);
        }else{
            f2Freq1.setTextColor(Color.RED);
            f2PulseWidth1Tv.setTextColor(Color.RED);
            f2RepeatPeriod1Tv.setTextColor(Color.RED);
            f2Power1Tv.setTextColor(Color.RED);
            f2Direction1Tv.setTextColor(Color.RED);
            f2North1Tv.setTextColor(Color.RED);
        }*/
    }

    public void f2SelectLine2(View view) {
        /*
        Log.d("MainActivity", "选中F2第2行");
        TextView f2Freq2 = (TextView) findViewById(R.id.f2Freq2);
        TextView f2PulseWidth2Tv = (TextView) findViewById(R.id.f2PulseWidth2Tv);
        TextView f2RepeatPeriod2Tv = (TextView) findViewById(R.id.f2RepeatPeriod2Tv);
        TextView f2Power2Tv = (TextView) findViewById(R.id.f2Power2Tv);
        TextView f2Direction2Tv = (TextView) findViewById(R.id.f2Direction2Tv);
        TextView f2North2Tv = (TextView) findViewById(R.id.f2North2Tv);

        if(f2Freq2.getCurrentTextColor() == Color.RED){
            f2Freq2.setTextColor(Color.BLACK);
            f2PulseWidth2Tv.setTextColor(Color.BLACK);
            f2RepeatPeriod2Tv.setTextColor(Color.BLACK);
            f2Power2Tv.setTextColor(Color.BLACK);
            f2Direction2Tv.setTextColor(Color.BLACK);
            f2North2Tv.setTextColor(Color.BLACK);
        }else{
            f2Freq2.setTextColor(Color.RED);
            f2PulseWidth2Tv.setTextColor(Color.RED);
            f2RepeatPeriod2Tv.setTextColor(Color.RED);
            f2Power2Tv.setTextColor(Color.RED);
            f2Direction2Tv.setTextColor(Color.RED);
            f2North2Tv.setTextColor(Color.RED);
        }*/
    }

    public void f2SelectLine3(View view) {
        /*
        Log.d("MainActivity", "选中F2第3行");
        TextView f2Freq3 = (TextView) findViewById(R.id.f2Freq3);
        TextView f2PulseWidth3Tv = (TextView) findViewById(R.id.f2PulseWidth3Tv);
        TextView f2RepeatPeriod3Tv = (TextView) findViewById(R.id.f2RepeatPeriod3Tv);
        TextView f2Power3Tv = (TextView) findViewById(R.id.f2Power3Tv);
        TextView f2Direction3Tv = (TextView) findViewById(R.id.f2Direction3Tv);
        TextView f2North3Tv = (TextView) findViewById(R.id.f2North3Tv);

        if(f2Freq3.getCurrentTextColor() == Color.RED){
            f2Freq3.setTextColor(Color.BLACK);
            f2PulseWidth3Tv.setTextColor(Color.BLACK);
            f2RepeatPeriod3Tv.setTextColor(Color.BLACK);
            f2Power3Tv.setTextColor(Color.BLACK);
            f2Direction3Tv.setTextColor(Color.BLACK);
            f2North3Tv.setTextColor(Color.BLACK);
        }else{
            f2Freq3.setTextColor(Color.RED);
            f2PulseWidth3Tv.setTextColor(Color.RED);
            f2RepeatPeriod3Tv.setTextColor(Color.RED);
            f2Power3Tv.setTextColor(Color.RED);
            f2Direction3Tv.setTextColor(Color.RED);
            f2North3Tv.setTextColor(Color.RED);
        }*/
    }

    public void f2SelectLine4(View view) {
        Log.d("MainActivity", "选中F2第4行");
    }

    public void f2SelectLine5(View view) {
        Log.d("MainActivity", "选中F2第5行");
    }

    public void f2SelectLine6(View view) {
        Log.d("MainActivity", "选中F2第6行");
    }

    public void f2SelectLine7(View view) {
        Log.d("MainActivity", "选中F2第7行");
    }

    public void f2SelectLine8(View view) {
        Log.d("MainActivity", "选中F2第8行");
    }

    public void f2SelectLine9(View view) {
        Log.d("MainActivity", "选中F2第9行");
    }

    public void f2SelectLine10(View view) {
        Log.d("MainActivity", "选中F2第10行");
    }

    /*====================================================================================================*/
    /*==============================================页面3=================================================*/
    /*====================================================================================================*/
    public void f3LocalFileSelect(View view) {
        Log.d("MainActivity", "本地文件选择");
/*
        Context content =getApplicationContext();
        Intent intent = new Intent();
        File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/data");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记
        intent.setAction(Intent.ACTION_GET_CONTENT);//动作，查看
        intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));//设置类型
        content.startActivity(intent);
*/
        //Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        //intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
        //intent.addCategory(Intent.CATEGORY_OPENABLE);
        //startActivityForResult(intent,1);

        File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/local");
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(file), "*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 2);
    }

    public void remoteFileSelect(View view) {
        Log.d("MainActivity", "远程文件选择");
        File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/remote");
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(file), "*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, 3);
    }

    public void convergenceLocate(View view){
        Log.d("MainActivity", "交汇定位");
    }

    public void resultSave(View view){
        Log.d("MainActivity", "结果存储");

        byte [] buf = new byte[12];
        buf[0] = 1;buf[1] = 1;buf[2] = 1;buf[3] = 1;buf[4] = 1;buf[5] = 1;
        buf[6] = 1;buf[7] = 1;buf[8] = 1;buf[9] = 1;buf[10] = 1;buf[11] = 1;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH∶mm∶ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        Context content =getApplicationContext();
        //saveFileToSDCardPrivateFilesDir(buf,"result",date,content);

        //String s = getSDCardPrivateFilesDir(content,"result");
        //Log.d("MainActivity", "读取文件路径:"+s);
    }

    public void f3LocalSelectLine1(View view) {
        Log.d("MainActivity", "选中F3本地第1行");
    }

    public void f3LocalSelectLine2(View view) {
        Log.d("MainActivity", "选中F3本地第2行");
    }
    public void f3LocalSelectLine3(View view) {
        Log.d("MainActivity", "选中F3本地第3行");
    }

    public void f3LocalSelectLine4(View view) {
        Log.d("MainActivity", "选中F3本地第4行");
    }

    public void f3LocalSelectLine5(View view) {
        Log.d("MainActivity", "选中F3本地第5行");
    }

    public void f3LocalSelectLine6(View view) {
        Log.d("MainActivity", "选中F3本地第6行");
    }

    public void f3LocalSelectLine7(View view) {
        Log.d("MainActivity", "选中F3本地第7行");
    }

    public void f3LocalSelectLine8(View view) {
        Log.d("MainActivity", "选中F3本地第8行");
    }

    public void f3LocalSelectLine9(View view) {
        Log.d("MainActivity", "选中F3本地第9行");
    }

    public void f3LocalSelectLine10(View view) {
        Log.d("MainActivity", "选中F3本地第10行");
    }

    public void f3RemoteSelectLine1(View view) {
        Log.d("MainActivity", "选中F3远程第1行");
    }

    public void f3RemoteSelectLine2(View view) {
        Log.d("MainActivity", "选中F3远程第2行");
    }

    public void f3RemoteSelectLine3(View view) {
        Log.d("MainActivity", "选中F3远程第3行");
    }

    public void f3RemoteSelectLine4(View view) {
        Log.d("MainActivity", "选中F3远程第4行");
    }

    public void f3RemoteSelectLine5(View view) {
        Log.d("MainActivity", "选中F3远程第5行");
    }

    public void f3RemoteSelectLine6(View view) {
        Log.d("MainActivity", "选中F3远程第6行");
    }

    public void f3RemoteSelectLine7(View view) {
        Log.d("MainActivity", "选中F3远程第7行");
    }

    public void f3RemoteSelectLine8(View view) {
        Log.d("MainActivity", "选中F3远程第8行");
    }

    public void f3RemoteSelectLine9(View view) {
        Log.d("MainActivity", "选中F3远程第9行");
    }

    public void f3RemoteSelectLine10(View view) {
        Log.d("MainActivity", "选中F3远程第10行");
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TextView f3Freq1 = (TextView) findViewById(R.id.f3Freq1);
        TextView f3PulseWidth1Tv = (TextView) findViewById(R.id.f3PulseWidth1Tv);
        TextView f3RepeatPeriod1Tv = (TextView) findViewById(R.id.f3RepeatPeriod1Tv);
        TextView f3Power1Tv = (TextView) findViewById(R.id.f3Power1Tv);
        TextView f3Direction1Tv = (TextView) findViewById(R.id.f3Direction1Tv);


        TextView f2Freq1 = (TextView) findViewById(R.id.f2Freq1);
        TextView f2PulseWidth1Tv = (TextView) findViewById(R.id.f2PulseWidth1Tv);
        TextView f2RepeatPeriod1Tv = (TextView) findViewById(R.id.f2RepeatPeriod1Tv);
        TextView f2Power1Tv = (TextView) findViewById(R.id.f2Power1Tv);
        TextView f2Direction1Tv = (TextView) findViewById(R.id.f2Direction1Tv);
        TextView f2North1Tv = (TextView) findViewById(R.id.f2North1Tv);

        TextView f2Freq2 = (TextView) findViewById(R.id.f2Freq2);
        TextView f2PulseWidth2Tv = (TextView) findViewById(R.id.f2PulseWidth2Tv);
        TextView f2RepeatPeriod2Tv = (TextView) findViewById(R.id.f2RepeatPeriod2Tv);
        TextView f2Power2Tv = (TextView) findViewById(R.id.f2Power2Tv);
        TextView f2Direction2Tv = (TextView) findViewById(R.id.f2Direction2Tv);
        TextView f2North2Tv = (TextView) findViewById(R.id.f2North2Tv);

        TextView f2Freq3 = (TextView) findViewById(R.id.f2Freq3);
        TextView f2PulseWidth3Tv = (TextView) findViewById(R.id.f2PulseWidth3Tv);
        TextView f2RepeatPeriod3Tv = (TextView) findViewById(R.id.f2RepeatPeriod3Tv);
        TextView f2Power3Tv = (TextView) findViewById(R.id.f2Power3Tv);
        TextView f2Direction3Tv = (TextView) findViewById(R.id.f2Direction3Tv);
        TextView f2North3Tv = (TextView) findViewById(R.id.f2North3Tv);

        if(requestCode == 1){
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                if (uri != null) {
                    String path = getPath(this, uri);

                    if (path != null) {
                        File file = new File(path);
                        if (file.exists()) {
                            String upLoadFilePath = file.toString();
                            String upLoadFileName = file.getName();
                            Log.d("MainActivity", "upLoadFilePath:"+upLoadFilePath);
                            Log.d("MainActivity", "upLoadFileName:"+upLoadFileName);

                            f2Freq1.setText("");
                            f2PulseWidth1Tv.setText("");
                            f2RepeatPeriod1Tv.setText("");
                            f2Power1Tv.setText("");
                            f2Direction1Tv.setText("");
                            f2North1Tv.setText("");

                            f2Freq2.setText("");
                            f2PulseWidth2Tv.setText("");
                            f2RepeatPeriod2Tv.setText("");
                            f2Power2Tv.setText("");
                            f2Direction2Tv.setText("");
                            f2North2Tv.setText("");

                            f2Freq3.setText("");
                            f2PulseWidth3Tv.setText("");
                            f2RepeatPeriod3Tv.setText("");
                            f2Power3Tv.setText("");
                            f2Direction3Tv.setText("");
                            f2North3Tv.setText("");

                            /*
                            byte [] buf = new byte[33];
                            buf = loadFileFromSDCard(upLoadFilePath);
                            System.out.println(Arrays.toString(buf));
                            if(buf.length<11) return;
                            //有一个数不为0且频率在范围之内，则显示数据
                            if(((buf[0]!=0)||(buf[1]!=0)||(buf[2]!=0)||(buf[3]!=0)||(buf[4]!=0)||(buf[5]!=0)||(buf[6]!=0)
                                    ||(buf[7]!=0)||(buf[8]!=0)||(buf[9]!=0)||(buf[10]!=0))&&(((buf[0]&0xff) + (((buf[1]&0xff) << 8)&0xff00))>=500)
                                    &&(((buf[0]&0xff) + (((buf[1]&0xff) << 8)&0xff00))<=18000)){
                                String freq1 = String.format(" %d",(buf[0]&0xff) + (((buf[1]&0xff) << 8)&0xff00));
                                f2Freq1.setText(freq1);
                                String pulseWidth1 = String.format(" %.0f",(float)(((buf[2]&0xff) + (((buf[3]&0xff) << 8)&0xff00))*0.01));
                                f2PulseWidth1Tv.setText(pulseWidth1);
                                String repeatPeriod1 = String.format(" %.0f",(float)(((buf[4]&0xff) + (((buf[5]&0xff) << 8)&0xff00))*0.2));
                                f2RepeatPeriod1Tv.setText(repeatPeriod1);

                                if((buf[6]&0xff)<100){
                                    String Power1 = String.format(" %d",(buf[6]&0xff));
                                    f2Power1Tv.setText("-"+Power1);
                                }else{
                                    String Power1 = String.format(" %d",((buf[6]&0xff)-100));
                                    f2Power1Tv.setText(Power1);
                                }

                                //String Power1 = String.format(" %d",(buf[6]&0xff));
                                //f2Power1Tv.setText(Power1);

                                //String Direction1 = String.format(" %.1f",(float)(((buf[7]&0xff) + (((buf[8]&0xff) << 8)&0xff00))*0.002));
                                String Direction1 = "";
                                if((buf[8]&0x80)==0x00){
                                    Direction1 = String.format(" %.1f",(float)((((buf[7]&0xff)+((buf[8]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }else if((buf[8]&0x80)==0x80){
                                    Direction1 = "-"+String.format(" %.1f",(float)((((buf[7]&0xff)+((buf[8]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }
                                f2Direction1Tv.setText(Direction1);
                                String North1 = String.format(" %.1f",(float)(((buf[9]&0xff) + (((buf[10]&0xff) << 8)&0xff00))*0.01));
                                f2North1Tv.setText(North1);
                            }else{
                                f2Freq1.setText("0");
                                f2PulseWidth1Tv.setText("0");
                                f2RepeatPeriod1Tv.setText("0");
                                f2Power1Tv.setText("0");
                                f2Direction1Tv.setText("0");
                                f2North1Tv.setText("0");
                            }

                            if(buf.length<22) return;
                            if(((buf[11]!=0)||(buf[12]!=0)||(buf[13]!=0)||(buf[14]!=0)||(buf[15]!=0)||(buf[16]!=0)||(buf[17]!=0)
                                    ||(buf[18]!=0)||(buf[19]!=0)||(buf[20]!=0)||(buf[21]!=0))&&(((buf[11]&0xff) + (((buf[12]&0xff) << 8)&0xff00))>=500)
                                    &&(((buf[11]&0xff) + (((buf[12]&0xff) << 8)&0xff00))<=18000)){
                                String freq2 = String.format(" %d",(buf[11]&0xff) + (((buf[12]&0xff) << 8)&0xff00));
                                f2Freq2.setText(freq2);
                                String pulseWidth2 = String.format(" %.0f",(float)(((buf[13]&0xff) + (((buf[14]&0xff) << 8)&0xff00))*0.01));
                                f2PulseWidth2Tv.setText(pulseWidth2);
                                String repeatPeriod2 = String.format(" %.0f",(float)(((buf[15]&0xff) + (((buf[16]&0xff) << 8)&0xff00))*0.2));
                                f2RepeatPeriod2Tv.setText(repeatPeriod2);

                                if((buf[17]&0xff)<100){
                                    String Power2 = String.format(" %d",(buf[17]&0xff));
                                    f2Power2Tv.setText("-"+Power2);
                                }else{
                                    String Power2 = String.format(" %d",((buf[17]&0xff)-100));
                                    f2Power2Tv.setText(Power2);
                                }

                                //String Power2 = String.format(" %d",(buf[17]&0xff));
                                //f2Power2Tv.setText(Power2);
                                //String Direction2 = String.format(" %.1f",(float)(((buf[18]&0xff) + (((buf[19]&0xff) << 8)&0xff00))*0.002));
                                String Direction2 = "";
                                if((buf[19]&0x80)==0x00){
                                    Direction2 = String.format(" %.1f",(float)((((buf[18]&0xff)+((buf[19]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }else if((buf[19]&0x80)==0x80){
                                    Direction2 = "-"+String.format(" %.1f",(float)((((buf[18]&0xff)+((buf[19]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }
                                f2Direction2Tv.setText(Direction2);
                                String North2 = String.format(" %.1f",(float)(((buf[20]&0xff) + (((buf[21]&0xff) << 8)&0xff00))*0.01));
                                f2North2Tv.setText(North2);
                            }else{
                                f2Freq2.setText("0");
                                f2PulseWidth2Tv.setText("0");
                                f2RepeatPeriod2Tv.setText("0");
                                f2Power2Tv.setText("0");
                                f2Direction2Tv.setText("0");
                                f2North2Tv.setText("0");
                            }

                            if(buf.length<33) return;
                            if(((buf[22]!=0)||(buf[23]!=0)||(buf[24]!=0)||(buf[25]!=0)||(buf[26]!=0)||(buf[27]!=0)||(buf[28]!=0)
                                    ||(buf[29]!=0)||(buf[30]!=0)||(buf[31]!=0)||(buf[32]!=0))&&(((buf[22]&0xff) + (((buf[23]&0xff) << 8)&0xff00))>=500)
                                    &&(((buf[22]&0xff) + (((buf[23]&0xff) << 8)&0xff00))<=18000)){
                                String freq3 = String.format(" %d",(buf[22]&0xff) + (((buf[23]&0xff) << 8)&0xff00));
                                f2Freq3.setText(freq3);
                                String pulseWidth3 = String.format(" %.0f",(float)(((buf[24]&0xff) + (((buf[25]&0xff) << 8)&0xff00))*0.01));
                                f2PulseWidth3Tv.setText(pulseWidth3);
                                String repeatPeriod3 = String.format(" %.0f",(float)(((buf[26]&0xff) + (((buf[27]&0xff) << 8)&0xff00))*0.2));
                                f2RepeatPeriod3Tv.setText(repeatPeriod3);


                                if((buf[28]&0xff)<100){
                                    String Power3 = String.format(" %d",(buf[28]&0xff));
                                    f2Power3Tv.setText("-"+Power3);
                                }else{
                                    String Power3 = String.format(" %d",((buf[28]&0xff)-100));
                                    f2Power3Tv.setText(Power3);
                                }

                                //String Power3 = String.format(" %d",(buf[28]&0xff));
                                //f2Power3Tv.setText(Power3);
                                //String Direction3 = String.format(" %.1f",(float)(((buf[29]&0xff) + (((buf[30]&0xff) << 8)&0xff00))*0.002));
                                String Direction3 = "";
                                if((buf[30]&0x80)==0x00){
                                    Direction3 = String.format(" %.1f",(float)((((buf[29]&0xff)+((buf[30]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }else if((buf[30]&0x80)==0x80){
                                    Direction3 = "-"+String.format(" %.1f",(float)((((buf[29]&0xff)+((buf[30]<<8)&0xff00))&0x7fff)*0.002));
                                    //Log.d("MainActivity", "测试-100:"+"-"+String.format(" %.1f",(float)(((msg.getBuf()[12]&0xff)+((msg.getBuf()[13]<<8)&0xff00))&0x7fff)));
                                }
                                f2Direction3Tv.setText(Direction3);
                                String North3 = String.format(" %.1f",(float)(((buf[31]&0xff) + (((buf[32]&0xff) << 8)&0xff00))*0.01));
                                f2North3Tv.setText(North3);
                            }else{
                                f2Freq3.setText("0");
                                f2PulseWidth3Tv.setText("0");
                                f2RepeatPeriod3Tv.setText("0");
                                f2Power3Tv.setText("0");
                                f2Direction3Tv.setText("0");
                                f2North3Tv.setText("0");
                            }
                            */


                            //字符串方案显示数据
                            //File file = new File("/storage/emulated/0/Android/data/com.soft.hwx.scdapp/files/manual");
                            String sRead = file2String(file,"utf8");
                            Log.d("MainActivity", "读取文件中的字符串:"+sRead);

                            if(!(sRead.equals("")||(sRead.length()==0))){
                                String substring = sRead.substring(0, sRead.length() - 1);
                                String[] split = substring.split(",");//以逗号分割
                                Log.d("MainActivity", "存储文件字符分割数:"+split.length);
                                if(split.length==18){
                                    try{
                                        f2Freq1.setText(split[0]);
                                        f2PulseWidth1Tv.setText(split[1]);
                                        f2RepeatPeriod1Tv.setText(split[2]);
                                        f2Power1Tv.setText(split[3]);
                                        f2Direction1Tv.setText(split[4]);
                                        f2North1Tv.setText(split[5]);

                                        f2Freq2.setText(split[6]);
                                        f2PulseWidth2Tv.setText(split[7]);
                                        f2RepeatPeriod2Tv.setText(split[8]);
                                        f2Power2Tv.setText(split[9]);
                                        f2Direction2Tv.setText(split[10]);
                                        f2North2Tv.setText(split[11]);

                                        f2Freq3.setText(split[12]);
                                        f2PulseWidth3Tv.setText(split[13]);
                                        f2RepeatPeriod3Tv.setText(split[14]);
                                        f2Power3Tv.setText(split[15]);
                                        f2Direction3Tv.setText(split[16]);
                                        f2North3Tv.setText(split[17]);
                                    }catch (Exception e){

                                    }
                                }
                            }

                            /*
                            String f2_freq1 = String.format(" %d",(buf[0]&0xff) + (((buf[1]&0xff) << 8)&0xff00));
                            f2Freq1.setText(f2_freq1);
                            String f2_PulseWidth1 = String.format(" %d",(buf[2]&0xff) + (((buf[3]&0xff) << 8)&0xff00));
                            f2PulseWidth1Tv.setText(f2_PulseWidth1);
                            String f2_RepeatPeriod1 = String.format(" %d",(buf[4]&0xff) + (((buf[5]&0xff) << 8)&0xff00));
                            f2RepeatPeriod1Tv.setText(f2_RepeatPeriod1);
                            String f2_Power1 = String.format(" %d",(buf[6]&0xff) + (((buf[7]&0xff) << 8)&0xff00));
                            f2Power1Tv.setText(f2_Power1);
                            String f2_Direction1 = String.format(" %d",(buf[8]&0xff) + (((buf[9]&0xff) << 8)&0xff00));
                            f2Direction1Tv.setText(f2_Direction1);
                            String f2_North1 = String.format(" %d",(buf[8]&0xff) + (((buf[9]&0xff) << 8)&0xff00));
                            f2North1Tv.setText(f2_North1);*/
                        }
                    }else{
                        Toast.makeText(this, getString(R.string.select_file_tip),Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }else if (requestCode == 2) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                if (uri != null) {
                    String path = getPath(this, uri);
                    if (path != null) {
                        File file = new File(path);
                        Log.d("MainActivity", "getPath:"+path);
                        if (file.exists()) {
                            String upLoadFilePath = file.toString();
                            String upLoadFileName = file.getName();
                            Log.d("MainActivity", "upLoadFilePath:"+upLoadFilePath);
                            Log.d("MainActivity", "upLoadFileName:"+upLoadFileName);

                            byte [] buf = new byte[12];
                            buf = loadFileFromSDCard(upLoadFilePath);
                            System.out.println(Arrays.toString(buf));
                            String freq1 = String.format(" %d",(buf[0]&0xff) + (((buf[1]&0xff) << 8)&0xff00));
                            f3Freq1.setText(freq1);
                            String PulseWidth1 = String.format(" %d",(buf[2]&0xff) + (((buf[3]&0xff) << 8)&0xff00));
                            f3PulseWidth1Tv.setText(PulseWidth1);
                            String RepeatPeriod1 = String.format(" %d",(buf[4]&0xff) + (((buf[5]&0xff) << 8)&0xff00));
                            f3RepeatPeriod1Tv.setText(RepeatPeriod1);
                            String Power1 = String.format(" %d",(buf[6]&0xff) + (((buf[7]&0xff) << 8)&0xff00));
                            f3Power1Tv.setText(Power1);
                            String Direction1 = String.format(" %d",(buf[8]&0xff) + (((buf[9]&0xff) << 8)&0xff00));
                            f3Direction1Tv.setText(Direction1);
                        }
                    }
                }
            }
        }else if(requestCode == 3){
            Log.d("MainActivity", "onActivityResult:远程文件选择");
        }
    }


    public void setAppConfig(View view){
        Intent i = new Intent("setting.LAUNCH");
        startActivity(i);
    }
    /*
    public void return_login(View view){
        Intent intent_Login = new Intent(MainActivity.this,Login.class) ;
        startActivity(intent_Login);
    }*/

}
